Unsure how to crop image using CICrop

一个人想着一个人 提交于 2019-12-10 20:18:07

问题


I'm trying to crop an image with CICrop, but when I try to crop, it crashes with this error message:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key CIAttributeTypeRectangle.' *** First throw call stack: (0x1847fc2d8 0x1964c80e4 0x1847fbf5c 0x184e34a4c 0x185635480 0x10005e554 0x10005eb7c 0x1892a9398 0x189292474 0x1892a8d34 0x1892a89c0 0x1892a1efc 0x189275120 0x1895162b8 0x189273634 0x1847b4240 0x1847b34e4 0x1847b1594 0x1846dd2d4 0x18e1336fc 0x1892daf40 0x10006481c 0x196b72a08) libc++abi.dylib: terminating with uncaught exception of type NSException

Here is my code:

@IBAction func CropButton(sender: UIButton)
{
   let CropBoxHeight = CGFloat(200)
   let CropBoxWidth = CGFloat(200)          
   let Rectangle = CIVector(x: view.center.x, y: view.center.y, z: 200, w: 200)
   let filter = CIFilter(name: "CICrop")
   let inputImage = CIImage(image: originalImage)

   let ciContext = CIContext(options: nil)

   filter.setDefaults()
   filter.setValue(inputImage, forKey: kCIInputImageKey)
   filter.setValue(Rectangle, forKey: kCIAttributeTypeRectangle)

   let originalOrientation: UIImageOrientation = imageView.image!.imageOrientation
   let originalScale = imageView.image!.scale

   let cgImage = ciContext.createCGImage(filter.outputImage, fromRect: inputImage.extent())

    imageView.image = UIImage(CGImage: cgImage, scale: originalScale, orientation: originalOrientation)!
}

It crashes on this line: filter.setValue(Rectangle, forKey: kCIAttributeTypeRectangle)


回答1:


It seems that you need to use

filter.setValue(Rectangle, forKey: "inputRectangle")

instead of

filter.setValue(Rectangle, forKey: kCIAttributeTypeRectangle)

This can be found in the official documentation. I did not manage to find any built-in constant for this key, however.



来源:https://stackoverflow.com/questions/31946075/unsure-how-to-crop-image-using-cicrop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!