下面的代碼列表會裁剪圖像。我希望圖像能夠變形/拉伸,而不是在形狀上裁剪。所有的圖像內容仍然在變換後的圖像中。它只會看起來不同。
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:"https://scontent-iad3-1.xx.fbcdn.net/v/t31.0-8/14196150_10207770295835716_3510332720585393213_o.jpg?oh=fdb525410602f40f4735991b713e9c50&oe=596688E5")!))!
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)
https://www.innofied.com/implementing-image-masking-in-ios/這正是我期待的但它似乎是在swift2,而不是迅速的3 –
如何如果您只是更改建議的編輯,則首先會比代碼更好地使用imageview。 –
好吧,我會給你演示.. –