ios - 如何将图像拉伸(stretch)成自定义形状(swift3)

标签 ios swift3 uiimageview shapes uibezierpath

enter image description here

下面的代码列表裁剪图像。我希望图像被转换/拉伸(stretch)而不是裁剪形状。所有图像内容仍然在转换后的图片中。它看起来会有所不同。

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)

最佳答案

看看这个线程:

Warp \ bend effect on a UIView?

讨论了几种解决方案,包括使用 Core Image 过滤器,或使用 OpenGL 构建您自己的“网格变形”。

还有 Brad Larson open source framework GPUImage on Github .您也许可以使用该框架构建一个过滤器来执行您想要的操作。

关于ios - 如何将图像拉伸(stretch)成自定义形状(swift3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42861078/

相关文章:

ios - Int' 不能转换为 'Range<Int>

ios - header 在 UITableView Controller 中不可见

iOS:获取图像的最大颜色

iOS混合模式相乘

ios - PINRemoteImage 删除缓存

ios - 如何在弹出窗口打开时打开 View Controller - swift 3

ios - 更新我的应用程序时可以更改屏幕截图吗?

ios - 如何在 swift 3 中创建像 Alamofire 这样的自定义闭包

ios - 将 AVPlayerViewController 放在 UIView 中

swift - 在 Swift 中初始化类属性