如何使用 Swift 仅圆化图像的某些角

How to round only some corners of an image with Swift

我只想将图像的两个底角圆化。 我在 Objective-C 中找到了很多示例,而在 Swift.

中几乎找不到任何示例

这是我找到的,但它给了我一个错误:

let rectShape = CAShapeLayer()

        rectShape.bounds = self.image.frame
        rectShape.position = self.image.center
        rectShape.path = UIBezierPath(roundedRect: self.image.bounds,     byRoundingCorners: .BottomLeft | .BottomRight, cornerRadii: CGSize(width: 20, height: 20)).CGPath

        self.image.layer.backgroundColor = UIColor.greenColor().CGColor

        self.image.layer.mask = rectShape

我得到的错误与 rectShape.path 一致,并告诉我

No '|' candidates produce the expected contextual result type 'UIRectCorner'

在 swift 中改变了我们这样放置事物的方式。在 objective c 中可以写一个 |分隔选项,但在 swift 中你必须像数组一样放置它:

let rectShape = CAShapeLayer()

    rectShape.bounds = self.image.frame
    rectShape.position = self.image.center
    rectShape.path = UIBezierPath(roundedRect: self.image.bounds,     byRoundingCorners: [.BottomLeft, .BottomRight], cornerRadii: CGSize(width: 20, height: 20)).CGPath

    self.image.layer.backgroundColor = UIColor.greenColor().CGColor

    self.image.layer.mask = rectShape
let path = UIBezierPath(roundedRect: self.bounds, byRoundingCorners: [.TopLeft, .BottomLeft], cornerRadii: CGSize(width: 3, height: 3 ))