【问题标题】:Set UIImageView Size in UITableViewCell when UIImage loaded from URL?从 URL 加载 UIImage 时在 UITableViewCell 中设置 UIImageView 大小?
【发布时间】:2012-08-04 13:25:34
【问题描述】:

将图像从 URL 加载到 uiimage 中,然后将这些图像添加到 uitableviewcell uiimageview,如下面的代码。我不知道图像大小,但需要在 tableviewcell 图像中强制它们为 40x40。图像在 uitableview 中以不同的宽度不断加载。

阅读与 uiimage 大小/缩放相关的其他帖子,但这些解决方案对我不起作用。我想这是因为我使用的是 uitableviewcell 中包含的 uiimageview 而不是创建自己的 uiimageview。

这是代码>

- (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;

}

我尝试过设置内容模式(尝试了aspectfill和aspect fit),尝试重置框架,设置autoresizingmask,clipTobound。没有任何改变。

【问题讨论】:

  • 我刚刚使用自定义 uiimageview 进行了尝试,效果很好。必须是 uitableviewcell 中包含的 uimageview 的东西

标签: ios uitableview uiimageview uiimage scale


【解决方案1】:

查看 Apple 示例项目“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;

【讨论】:

  • 我试过了,但收到一条消息“您必须等待 2 天才能接受自己的答案”。
  • 你要把这个放在哪个类的什么消息里?这是在您的 UITableView 或 UITableViewCell 的子类中吗?
  • 上面的代码在主线程的'tableView:cellForRowAtIndexPath:'中,但后来我切换我的代码来模仿苹果开发网站上的'lazy table images'中的示例,即调用背景thread from 'tableView: cellForRowAtIndexPath:' 然后下载图像,将其保存到 uiimage 中,完成后,将 uiimage 返回到主线程以放入 cell.imageView。我认为 icondownloader 是示例项目中在后台运行的对象的名称。
  • 谢谢,我试试看。我遇到的确切问题在这里得到了更准确的描述:stackoverflow.com/questions/8085750/… 但不幸的是,这个问题没有得到任何答案。
  • 我试过这个。它有效,但图像变得模糊!我还尝试了以下帖子的第一个答案,效果更好(不会使图像模糊):stackoverflow.com/questions/2788028/…
【解决方案2】:

我遇到了同样的问题,这对我来说非常有效;

选择 UIImage 并转到属性检查器并检查“剪辑子视图”

确保为图像的大小设置了约束。

【讨论】:

    【解决方案3】:

    当我错误地使用单元格的 imageView 属性而不是我在情节提要中创建的自定义 UIImageView 的属性时,我遇到了这个问题。当我使用对自定义视图的正确引用时,一切都按预期工作。我怀疑你们中的一些人可能做过类似的事情。

    【讨论】:

    • 可以吻你。为我修复
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 2013-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-16
    • 2011-02-11
    相关资源
    最近更新 更多