2017-03-16 67 views

回答

0

嘗試這樣的:

extension UIImageView { 
    func addMask(_ bezierPath: UIBezierPath) { 
     let pathMask = CAShapeLayer() 
     pathMask.path = bezierPath.cgPath 
     layer.mask = pathMask 
    } 
} 

測試操場:

let myPicture = UIImage(data: try! Data(contentsOf: URL(string:"http://i.stack.imgur.com/Xs4RX.jpg")!))! 
let iv = UIImageView(image: myPicture) 

let bezierPath = UIBezierPath() 
bezierPath.move(to: iv.center) 
bezierPath.addLine(to: CGPoint(x: iv.frame.maxX, y: 0)) 
bezierPath.addLine(to: CGPoint(x: iv.frame.maxX, y: iv.frame.maxY)) 
bezierPath.addLine(to: CGPoint(x: 0, y: iv.frame.maxY)) 
bezierPath.addLine(to: .zero) 
bezierPath.close() 

iv.addMask(bezierPath) 
+0

我是否將測試操場放入擴展文件中? –

+0

您需要在您的操場文件中的測試代碼 –

+0

在你的項目,你可以添加一個新的快捷文件之前有添加擴展,進口的UIKit添加到該文件,並添加擴展名, –