【问题标题】:Set image size after load加载后设置图像大小
【发布时间】:2016-01-13 02:09:40
【问题描述】:

我在单元格中有一个 UIImageView,默认图像和大小为 80x80 和约束。然后在表格视图中我动态加载不同大小的新图像:

if let url = NSURL(string: currentStoreProduct.productImageUrl) {
                if let data = NSData(contentsOfURL: url) {
                    cell.imageView!.image = UIImage(data: data)
                }
            }

但是当我将此图像设置为我的 imageView 时,图像不适合以前的大小。我想将此图像缩放到默认图像的当前宽度和高度。我试过这个:

cell.imageView!.frame = CGRectMake(cell.imageView!.frame.origin.x, cell.imageView!.frame.origin.y, 80, 80)

但这并没有帮助。

【问题讨论】:

  • 如果你对图像视图有约束,那么你需要通过编辑约束来调整它的大小。如果您更改框架,它将被下一次布局传递中的约束值覆盖。

标签: ios image swift swift2


【解决方案1】:

我相信你应该试试

imageView.clipsToBounds = true
imageView.contentMode = UIViewContentMode.ScaleToFill

第二个可能不同(这取决于您的需要),您可以使用 .ScaleAspectFit、.ScaleAspectFill 等

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-12-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多