How to apply CIPhotoEffectMono filter on image in iOS?

血红的双手。 提交于 2019-12-12 11:45:38

问题


This is my code for filter:

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

Here is the error message:

Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[ setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key inputIntensity.'

First throw call stack: (

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: terminating with uncaught exception of type NSException (lldb)


回答1:


CIPhotoEffectMono doesn't support kCIInputIntensityKey. In fact none of the photo effect filters have any inputs apart from input image. If you remove filter!.setValue(0.3, forKey: kCIInputIntensityKey) your code should work fine.

You can check the supported inputs of a filter with filter.inputKeys which returns an array of strings containing the names of all the inputs.

Simon



来源:https://stackoverflow.com/questions/38395197/how-to-apply-ciphotoeffectmono-filter-on-image-in-ios

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