【问题标题】:iphone: Scale the image in table celliphone:缩放表格单元格中的图像
【发布时间】:2013-01-22 02:43:33
【问题描述】:

我正在使用 UITableViewCellStyleSubTitle。我正在尝试将缩略图添加到我的表格单元格中。 UIViewContentModeScaleAspectFit 似乎无法正常工作,原因不明。我在堆栈溢出中看到了很多类似问题的答案。如果我使用 drawInRect 裁剪图像,它可以工作,就像这样

[thumbnail drawInRect:thumbnailRect];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

但我想要达到的目标不应该需要我这样做。我只是想缩放图像而不是裁剪它。 因此,将默认单元格的图像视图的内容模式设置为“UIViewContentModeScaleAspectFit”应该可以工作..但它没有。

cell.imageView.contentMode = UIViewContentModeScaleAspectFit;

设置缩略图的完整逻辑..fPath是图片的路径

UIImage *thumbnail = [UIImage imageWithContentsOfFile:fPath];
[cell.imageView setImage:thumbnail];
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.clipsToBounds = YES;

【问题讨论】:

  • 尝试 subclussin UITableViewCell 或在代码中创建 UIImageView 并将其添加到单元格的 contentView 中。
  • 那会起作用..但这不是我想要实现的开销..吗?我想要一个标准单元..

标签: iphone uitableview size image-scaling


【解决方案1】:

我发现 drawInRect 如果尺寸较小则拉伸原始图像,如果尺寸较大则裁剪它。

本文将帮助您准备图片的缩略图 http://vocaro.com/trevor/blog/2009/10/12/resize-a-uiimage-the-right-way/

它有几个有用的 UIImage 类扩展: * UIImage+调整大小 * UIImage+Alpha * UIImage+圆角

示例用法:

UIImage *imageThumb = [YourImage thumbnailImage:70 transparentBorder:0 cornerRadius:10 interpolationQuality:kCGInterpolationHigh];

然后你可以使用 drawInRect 方法。如果图像不是矩形 - 图像将裁剪较大的一侧并取图像的中间

希望这个答案能指导你。

【讨论】:

    猜你喜欢
    • 2011-02-12
    • 1970-01-01
    • 2018-03-30
    • 1970-01-01
    • 2013-02-09
    • 2010-11-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多