简体   繁体   English

从URL加载UIImage时,在UITableViewCell中设置UIImageView Size?

[英]Set UIImageView Size in UITableViewCell when UIImage loaded from URL?

Loading images from URL into a uiimage and then adding those images to a uitableviewcell uiimageview, like below code. 将图像从URL加载到uiimage中,然后将这些图像添加到uitableviewcell uiimageview,如下面的代码。 I do not know the image sizes, but need to force them to be 40x40 in the tableviewcell image. 我不知道图像大小,但需要在tableviewcell图像中强制它们为40x40。 The images keep loading with different widths in the uitableview. 图像在uitableview中以不同的宽度加载。

Read through other posts related to uiimage sizing/scaling, but those solutions don't work for me. 阅读与uiimage尺寸/缩放相关的其他帖子,但这些解决方案对我不起作用。 I am thinking it's because I am using the uiimageview included in uitableviewcell vs creating my own uiimageview. 我在想这是因为我正在使用uitableviewcell中包含的uiimageview来创建我自己的uiimageview。

Here's the code> 这是代码>

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

UITableViewCell *cell = [[UITableViewCell alloc] init];

ItemForSale *item = [listOfItems objectAtIndex:indexPath.row];
cell.textLabel.text = item.name;
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.font = [UIFont systemFontOfSize:14];

NSURL *url = [NSURL URLWithString: item.imgPath];
UIImage *thumbnail = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
if (thumbnail == nil) {
    thumbnail = [UIImage imageNamed:@"noimage.png"] ;
}
cell.imageView.image = thumbnail;
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.frame = CGRectMake(0, 0, 40, 40);
cell.imageView.autoresizingMask = UIViewAutoresizingNone;
cell.imageView.clipsToBounds = YES;

return cell;

}

I've tried setting content mode (tried both aspectfill and aspect fit), tried resetting the frame, setting autoresizingmask, clipTobound. 我尝试过设置内容模式(尝试了aspectfill和aspect fit),尝试重置框架,设置autoresizingmask,clipTobound。 none of it changed a thing. 没有一件事改变了。

Found the answer looking at Apples example project "LazyTableImages" 找到苹果示例项目“LazyTableImages”的答案

NSURL *url = [NSURL URLWithString: item.imgPath];
UIImage *thumbnail = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
if (thumbnail == nil) {
    thumbnail = [UIImage imageNamed:@"noimage.png"] ;
}
CGSize itemSize = CGSizeMake(40, 40);
UIGraphicsBeginImageContext(itemSize);
CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[thumbnail drawInRect:imageRect];
cell.imageView.image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return cell;

I had the same problem and this worked perfectly for me; 我遇到了同样的问题,这对我来说非常合适;

select the UIImage and go to attributes inspector and check "Clip Subviews" 选择UIImage并转到属性检查器并选中“剪辑子视图”

make sure you have constraints set for the image's size. 确保为图像的大小设置了约束。

I had this problem when I was mistakenly using the imageView property of the cell rather than the property for the custom UIImageView I created in the storyboard. 当我错误地使用单元格的imageView属性而不是我在故事板中创建的自定义UIImageView的属性时,我遇到了这个问题。 When I used the proper reference to the custom view, it all worked as expected. 当我使用对自定义视图的正确引用时,它都按预期工作。 I suspect some of you may have done similar. 我怀疑你们中的一些人可能做过类似的事。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM