如何在iOS上的图像上应用CIPhotoEffectMono过滤器?

时间:2016-07-15 11:39:02

标签: ios swift core-image

这是我的过滤器代码:

let filter = CIFilter(name: "CIPhotoEffectMono")
filter!.setValue(CIImage(image: imageView.image!) , forKey: kCIInputImageKey)
filter!.setValue(0.3, forKey: kCIInputIntensityKey)
let context = CIContext(options:nil)
let cgimg = context.createCGImage(filter!.outputImage!, fromRect: filter!.outputImage!.extent)
let newImage = UIImage(CGImage:cgimg)
self.imageView.image = newImage

以下是错误消息:

  

由于未捕获的异常'NSUnknownKeyException'而终止应用程序,原因:'[setValue:forUndefinedKey:]:此类不是键输入密度的键值编码兼容。'

     

第一次抛出调用堆栈:   (

     

0 CoreFoundation 0x0000000105f9af65 __exceptionPreprocess + 165

     

1 libobjc.A.dylib 0x0000000107f56deb objc_exception_throw + 48

     

2 CoreFoundation 0x0000000105f9aba9 - [NSException raise] + 9

     

3 CoreImage 0x0000000106354f7a - [CIFilter setValue:forUndefinedKey:] + 137

     

4 Foundation 0x000000010668af5b - [NSObject(NSKeyValueCoding)setValue:forKey:] + 288

     

5 MyFirstApp 0x0000000105a26bac _TFC10MyFirstApp14ViewController13lightBlendBtnfS0_FPSs9AnyObject_T_ + 988

     

6 MyFirstApp 0x0000000105a27076 _TToFC10MyFirstApp14ViewController13lightBlendBtnfS0_FPSs9AnyObject_T_ + 54

     

7 UIKit 0x0000000106ae01fa - [UIApplication sendAction:to:from:forEvent:] + 92

     

8 UIKit 0x0000000106c44504 - [UIControl sendAction:to:forEvent:] + 67

     

9 UIKit 0x0000000106c447d0 - [UIControl _sendActionsForEvents:withEvent:] + 311

     

10 UIKit 0x0000000106c43906 - [UIControl touchesEnded:withEvent:] + 601

     

11 UIKit 0x0000000106b4aaa3 - [UIWindow _sendTouchesForEvent:] + 835

     

12 UIKit 0x0000000106b4b691 - [UIWindow sendEvent:] + 865

     

13 UIKit 0x0000000106afd752 - [UIApplication sendEvent:] + 263

     

14 UIKit 0x00000001140f4a55 - [UIApplicationAccessibility sendEvent:] + 77

     

15 UIKit 0x0000000106ad8fcc _UIApplicationHandleEventQueue + 6693

     

16 CoreFoundation 0x0000000105ec70a1 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION + 17

     

17 CoreFoundation 0x0000000105ebcfcc __CFRunLoopDoSources0 + 556

     

18 CoreFoundation 0x0000000105ebc483 __CFRunLoopRun + 867

     

19 CoreFoundation 0x0000000105ebbe98 CFRunLoopRunSpecific + 488

     

20 GraphicsServices 0x000000010cdccad2 GSEventRunModal + 161

     

21 UIKit 0x0000000106ade676 UIApplicationMain + 171

     

22 MyFirstApp 0x0000000105a29fed main + 109

     

23 libdyld.dylib 0x0000000108a8292d start + 1

     

     

libc ++ abi.dylib:以NSException类型的未捕获异常终止   (lldb)

1 个答案:

答案 0 :(得分:2)

CIPhotoEffectMono不支持kCIInputIntensityKey。实际上,除了输入图像之外,没有一个光效滤波器具有任何输入。如果您删除filter!.setValue(0.3, forKey: kCIInputIntensityKey),您的代码应该可以正常工作。

您可以使用filter.inputKeys检查过滤器支持的输入,该过滤器返回包含所有输入名称的字符串数组。

西蒙