【问题标题】:UIImage Scale Aspect Fill and Clip Subviews not working in UICollectionViewUIImage Scale Aspect Fill and Clip Subviews 在 UICollectionView 中不起作用
【发布时间】:2015-07-10 01:23:47
【问题描述】:

我有一个 UICollectionView,其中一个单元格包含一个 UIImageView。我正在设置 UIImageView frame.size 以匹配单元格的 frame.size 并在 cellForItemAtIndexPath 方法中以以下方式明确要求 UIImageView 使用 Aspect Fill & Clip Subview 进行缩放:

let cell:UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! UICollectionViewCell

... 

var imageView = UIImageView(image: self.eventImages[indexPath.row])
imageView.frame.size = cell.frame.size
cell.contentMode = UIViewContentMode.ScaleAspectFill
cell.clipsToBounds = true
imageView.contentMode = UIViewContentMode.ScaleAspectFill
imageView.clipsToBounds = true
cell.addSubview(imageView)

结果是一个拉伸的图像(IMAGE A),但是当我单击(点击)图像时,它“神奇地”解决了Aspect Fill & Clip Subviews (IMAGE B)。我似乎无法理解为什么只有在点击后才会发生这种情况,而不是在第一次加载图像时发生。它们被异步获取到服务器,但缩放仅在点击图像后发生(也没有实现点击功能)。

注意:UICollectionView 设置在故事板视图控制器中,集合视图和可重用单元都启用了方面填充和剪辑子视图。

图像 A(缩放错误 - 已拉伸且未裁剪):

图像 B(点击后正确缩放):

【问题讨论】:

    标签: ios iphone uiimageview uicollectionview


    【解决方案1】:

    这样做很简单:

    imageView.contentMode = UIViewContentMode.ScaleAspectFill;
    imageView.layer.masksToBounds = YES;
    

    【讨论】:

    • 应该是 imageView.layer.masksToBounds = YES;
    • 你还看到了什么,我写的?
    • 斯威夫特 4 = imageView.layer.maskToBounds = true
    【解决方案2】:

    我认为你应该使用:

    imageView.contentMode = UIViewContentMode.ScaleAspectFill
    imageView.clipsToBounds = true
    

    而不是这个:

      cell.contentMode = UIViewContentMode.ScaleAspectFill
      cell.clipsToBounds = true
    

    【讨论】:

    • 不,两者都有,但也只是尝试评论单元格缩放并只留下 imageView 缩放但没有用。在故事板中,Collection View 和 Collection Reusable Cell 都有 Aspect FillClip Subviews... 有什么不同的建议可以在那里工作吗?
    【解决方案3】:

    [yourImageView setContentMode:UIViewContentModeScaleAspectFill]; yourImageView.layer.masksToBounds = YES;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-07
      • 2015-07-29
      • 2020-07-22
      • 2018-04-01
      • 2022-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多