Swift 4 - 在swift中有两个圆角的imageview?

Tun*_*dır 3 uiimageview ios uicollectionview swift swift4

我在UICollectionView Cell中有一个UIImageView,我想让它的顶角变圆.我无法解决,任何帮助都表示赞赏.

你可以看到单元格有10px的角半径,我也希望在图像上应用相同的效果.

Endeksa

Ash*_*iya 14

你可以在这里试试UIRectCorner Document

这里我的自定义类AGRoundCornersView

extension UIImageView {
    public func roundCorners(_ corners: UIRectCorner, radius: CGFloat) {
        let maskPath = UIBezierPath(roundedRect: bounds,
                                    byRoundingCorners: corners,
                                    cornerRadii: CGSize(width: radius, height: radius))
        let shape = CAShapeLayer()
        shape.path = maskPath.cgPath
        layer.mask = shape
    }
}
Run Code Online (Sandbox Code Playgroud)

代码:

1.调整视图大小时调用.

uiimage.roundCorners([.topLeft, .topRight], radius: 10)
Run Code Online (Sandbox Code Playgroud)

2.创建自定义类

class CustomImageView: UIImageView {
    override func layoutSubviews() {
        super.layoutSubviews()
        self.roundCorners([.topLeft, .topRight], radius: 10)
    }
}
Run Code Online (Sandbox Code Playgroud)