【问题标题】:Unsure how to crop image using CICrop不确定如何使用 CICrop 裁剪图像
【发布时间】:2015-11-03 22:23:10
【问题描述】:

我正在尝试使用 CICrop 裁剪图像,但是当我尝试裁剪时,它会崩溃并显示以下错误消息:

由于未捕获的异常“NSUnknownKeyException”而终止应用程序, 原因:'[setValue:forUndefinedKey:]: 这个类 与键的键值编码不兼容 CIAttributeTypeRectangle。 *** 第一次抛出调用堆栈:(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:以未捕获的类型异常终止 NSException

这是我的代码:

@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)!
}

它在这一行崩溃: filter.setValue(Rectangle, forKey: kCIAttributeTypeRectangle)

【问题讨论】:

    标签: ios swift filter camera crop


    【解决方案1】:

    看来你需要使用

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

    而不是

    filter.setValue(Rectangle, forKey: kCIAttributeTypeRectangle)
    

    这可以在official documentation 中找到。但是,我没有为这个键找到任何内置常量。

    【讨论】:

      猜你喜欢
      • 2012-05-08
      • 1970-01-01
      • 1970-01-01
      • 2014-11-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多