Comment appliquer le filter CIPhotoEffectMono sur une image dans iOS?

Ceci est mon code pour le 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 

Voici le message d'erreur:

Terminaison de l'application en raison d'une exception non interceptée 'NSUnknownKeyException', raison: '[setValue: forUndefinedKey:]: cette class n'est pas conforme à la valeur key encoding-conforme pour la key inputIntensity.'

Pile d'appel du premier lancer: (

0 CoreFoundation 0x0000000105f9af65 __exceptionPreprocess + 165

1 libobjc.A.dylib 0x0000000107f56deb objc_exception_throw + 48

2 CoreFoundation 0x0000000105f9aba9 – [Augmentation NSException] + 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: à: de: pourEvent:] + 92

8 UIKit 0x0000000106c44504 ​​- [UIControl sendAction: à: pourEvent:] + 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 démarrer + 1

)

libc ++ abi.dylib: se termine avec une exception non interceptée de type NSException (lldb)

CIPhotoEffectMono ne prend pas en charge kCIInputIntensityKey . En fait, aucun des filters d'effets photo n'a d'inputs en dehors de l'image d'input. Si vous supprimez le filter!.setValue(0.3, forKey: kCIInputIntensityKey) votre code devrait fonctionner filter!.setValue(0.3, forKey: kCIInputIntensityKey) .

Vous pouvez vérifier les inputs sockets en charge d'un filter avec filter.inputKeys qui renvoie un tableau de strings contenant les noms de toutes les inputs.

Simon