【问题标题】:Set rounded corners on UIimage in UICollectionViewCell in swift快速在 UICollectionViewCell 中的 UIimage 上设置圆角
【发布时间】:2017-02-09 12:06:00
【问题描述】:

我有一个简单的问题,在谷歌、文档或此处找不到解决方案。

我的视图控制器中有一个 Collectionview。我创建了一个自定义单元,DescriptionCell,其中包含一个 UIImage。我希望这张图片有圆角。但是,我不知道在哪里设置 UIImage 层上的cornerradius。我在单元格的 awakeFromNib 方法、委托方法 CellForRowAtIndexPath 中尝试过,并在单元格中覆盖了 LayoutSubview 但它不起作用。 设置 UIImage 半径的代码应该放在哪里?

要指定,我知道如何创建 UIImage 的圆角。但是如果是Collectionview单元格的子视图,不知道在哪里设置cornerradius。

这是我的 descriptionCell 的代码

class DescriptionCell: UICollectionViewCell {
    @IBOutlet weak var mImage: UIImageView!

    override func awakeFromNib() {
    //mImage.layer.cornerradius = 5 
    //Does not work, the image is still square in the cell
    }

在单元格中为RowAtIndePath

 func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
var cell = collectionView.dequeueReusableCellWithReuseIdentifier("descriptioncell", forIndexPath: indexPath) as! DescriptionCell
    //cell.mImage.layer.cornerradius = 5 
    //Does not work, the image is still square in the cell
return cell    
}

提前致谢!

【问题讨论】:

  • 不,这是不同的。我已经在使用该答案中的代码。我的问题是 UIImage.layer.cornerradius = xxx 放在哪里?
  • 用相关代码更新您的问题。
  • 没有可以添加的特定代码。我的问题很简单:如何在 UICollectionViewCell 中创建一个带有圆角的 UIImage。
  • @Jell92 至少为您的DescriptionCellCellForRowAtIndexPath 添加代码是有意义的。

标签: ios swift uiimage viewcontroller collectionview


【解决方案1】:

您是否尝试过将其放在自定义 UICollectionViewCell 的 init 函数中?

override init(frame: CGRect) {
    super.init(frame: frame)

    image.layer.masksToBounds = true
    image.layer.cornerRadius = 10
}

【讨论】:

    【解决方案2】:

    嗯,您正在使用您所说的答案中的部分代码。 另一部分是imageView.clipsToBounds = true

    像这样更新你的awakeFromNib

    override func awakeFromNib() {
        mImage.layer.cornerRadius = 5 
        mimage.clipsToBounds = true
    }
    

    要使其成为圆形,您需要将cornerRadius 设置为正方形高度的一半。在您的cellForItemAtIndexPath 中添加以下几行:

    cell.layoutIfNeeded()
    cell.mImage.layer.cornerRadius = cell.mImage.frame.height/2
    

    更新

    为避免layoutSubviews 被调用两次,请在DescriptionCell 类中覆盖layoutSubviews 并将代码放在那里:

    override func layoutSubviews() {
        super.layoutSubviews()
        layoutIfNeeded()
        mImage.layer.cornerRadius = mImage.frame.height/2
    }
    

    【讨论】:

    • 我认为成功了!然而现在又出现了另一个问题,我希望它是圆形的。我知道代码类似于: mImage.layer.cornerRadius = mImage.frame.width/2 但是当我使用它时它不起作用。图像消失!
    • 在调用awakeFromNib 之后,需要根据框架设置圆角半径。需要在调整单元格大小并设置图像后完成。
    • 感谢您的回答!那到底是什么时候?那我可以用什么方法设置呢?
    • 你可以在cellForItemAtIndexPath或者willDisplayCell的单元格上调用一些配置函数
    • 嗯,好吧,但是如果 LayoutIfNeeded() 调用我们的 LayoutSubviews 实现,那不只是递归 - 因为 LayoutSubviews 会调用自己?
    【解决方案3】:

    您还可以创建一个扩展,例如:

    extension UIView {
        func addBorder(color: UIColor, cornerRadius: CGFloat = 10, borderWidth: CGFloat = 1.0) {
            layer.borderWidth = borderWidth;
            layer.borderColor = color.cgColor
            layer.cornerRadius = cornerRadius
            layer.masksToBounds = true
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-17
      • 1970-01-01
      • 2019-02-17
      • 2010-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-12
      相关资源
      最近更新 更多