2017-03-16 112 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添加到该文件,并添加扩展名, –