我正在尝试将单个imageview转换为在右侧对象中的输出。这是与How to make imageview with different shape in swift非常相似的问题。但是那很快。我试过了,它似乎在swift3中不起作用。
最佳答案
尝试这样:
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)
关于ios - 如何将 ImageView 转换为自定义形状(swift3),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42843318/