I'm using CIFilter's CIPerspectiveCorrection to crop an image, but the part I want to crop is not always a perfect rectangle so the cropped image returns distorted.
let filter: CIFilter = CIFilter(name: "CIPerspectiveCorrection")!
filter.setValue(CIImage(cgImage: inputImage.cgImage!), forKey: "inputImage")
filter.setValue(CIVector(cgPoint: topLeft), forKey: "inputTopLeft")
filter.setValue(CIVector(cgPoint: topRight), forKey: "inputTopRight")
filter.setValue(CIVector(cgPoint: bottomLeft), forKey: "inputBottomLeft")
filter.setValue(CIVector(cgPoint: bottomRight), forKey: "inputBottomRight")
let cutImageRef = CIContext(options: nil).createCGImage(filter.outputImage!, from: filter.outputImage!.extent)!
// Return image to UIImage
let croppedImage: UIImage = UIImage(cgImage: cutImageRef)
return croppedImage
How do I return the image as is on the 2d plane without stretching to fill a rectangle?
If you want just the inside of the polygon as is, you can use the Core Graphics APIs for masking the image with a path. Maybe you can find some inspiration in this question.