Ios 调整TableViewController单元格的图像大小
我想调整所有TableView单元格的图像大小。 我尝试了这种方法,它的工作,但图像质量下降Ios 调整TableViewController单元格的图像大小,ios,objective-c,uitableview,uiimageview,uiimage,Ios,Objective C,Uitableview,Uiimageview,Uiimage,我想调整所有TableView单元格的图像大小。 我尝试了这种方法,它的工作,但图像质量下降 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { //... CGSize size = {35,35}; cell.imageView.image = [self imageWithImage:[UIImag
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//...
CGSize size = {35,35};
cell.imageView.image = [self imageWithImage:[UIImage imageNamed:@"image.png"] scaledToSize:size];
//...
}
//Given a UIImage and a CGSize, this method will return a resized UIImage.
- (UIImage*)imageWithImage:(UIImage*)image
scaledToSize:(CGSize)newSize;
{
UIGraphicsBeginImageContext( newSize );
[image drawInRect:CGRectMake(0,0,35,35)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
所以我继续搜索,我发现有人说要添加这个方法
- (void)layoutSubviews {
[super layoutSubviews];
self.imageView.frame = CGRectMake(0,0,35,35);
}
但是Xcode给了我一个错误,这个方法不再可用了吗
如何调整图像大小而不降低质量?我设法找到了一个解决方案,这里我将解释一下,以防有人需要 首先创建一个假图像,比如白色图像或透明图像
cell.imageView.image = [UIImage imageNamed:@"clear.png"];
这就是为什么你必须告诉细胞有一个图像。
现在添加一个ImageView
作为TableView
单元格的子视图
UIImageView *imgView=[[UIImageView alloc] initWithFrame:CGRectMake(20, 5, 35, 35)];
imgView.backgroundColor=[UIColor clearColor];
[imgView.layer setCornerRadius:8.0f];
[imgView.layer setMasksToBounds:YES];
[imgView setImage:[UIImage imageNamed:account.icon]];
[cell.contentView addSubview:imgView];
现在好了。如果您不创建假图像,新创建的
imgView
将与单元格.textLabel.text
重叠,XCode会给您带来什么错误?质量下降是否意味着图像变得模糊?@Larme error“无可见界面…声明选择器布局子视图”`@cocooalog!事实上,在应用程序的另一部分中,我将图像调整为35x35,质量很好,但这里的图像模糊,请猜测方法UIGraphicsGetImageFromCurrentImageContext()。将UIGraphicsBeginImageContext(newSize)
替换为UIGraphicsBeginImageContextWithOptions(newSize,NO,0.0)
,您应该很好。每次单元格显示时,您都会创建一个新的图像视图,并且从不删除/释放它们。那会引起一些严重的麻烦。