下面的代码列表会裁剪图像。我希望图像被转换/拉伸,而不是在形状中裁剪。所有图像内容仍然在转换后的图片中。它看起来会有所不同。
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)
答案 0 :(得分:0)
看一下这个帖子:
Warp \ bend effect on a UIView?
讨论了几种解决方案,包括使用核心图像过滤器,或构建自己的"网格扭曲"使用OpenGL。
还有Brad Larson open source framework GPUImage on Github。您可以使用该框架构建过滤器以执行您想要的操作。
答案 1 :(得分:0)
当您必须显示该类型图像时,您可以使用图像过滤器核心,或者您可以尝试图像蒙版..
图像核心过滤器:显示在:
https://code.tutsplus.com/tutorials/ios-sdk-apply-photo-filters-with-core-image-in-swift--cms-27142
图像蒙版:
https://www.innofied.com/implementing-image-masking-in-ios/
我尽我所能......提供解决方案..这是简单的形象
是gif类型图像的掩模图像
编写
的代码override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let image = UIImage(named: "image.gif")
let maskingImage = UIImage(named: "mask-image.gif")
imageView.image = maskImage(image: image!, mask: maskingImage!)
}
func maskImage(image:UIImage, mask:(UIImage))->UIImage{
let imageReference = image.cgImage
let maskReference = mask.cgImage
let imageMask = CGImage(maskWidth: maskReference!.width,
height: maskReference!.height,
bitsPerComponent: maskReference!.bitsPerComponent,
bitsPerPixel: maskReference!.bitsPerPixel,
bytesPerRow: maskReference!.bytesPerRow,
provider: maskReference!.dataProvider!, decode: nil, shouldInterpolate: true)
let maskedReference = imageReference!.masking(imageMask!)
let maskedImage = UIImage(cgImage:maskedReference!)
return maskedImage
}