我正在使用 CIFilter 的 CIPerspectiveCorrection 来裁剪图像,但我想要裁剪的部分并不总是一个完美的矩形,因此裁剪后的图像返回失真。
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
如何在 2d 平面上按原样返回图像而不拉伸(stretch)以填充矩形?
最佳答案
如果你只想要多边形的内部,你可以使用 Core Graphics APIs 来用路径屏蔽图像。
也许你可以在 this question 中找到一些灵感.
关于ios - CIPerspectiveCorrection 按原样返回图像,未拉伸(stretch),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57119302/