2017-03-04 1 views
0

J'écrivais le code pour ajouter des filtres aux images. Voici le codeNSInvalidArgumentException lors de l'application du filtre

func applyFilter(index: Int){ 
    print("Applying Filter....") 
    //let context = CIContext() 
    // 1 
    //below two lines will refer the code to GPU instead of cpu 
    let openGLContext = EAGLContext(api: .openGLES2) 
    let context = CIContext(eaglContext: openGLContext!) 
    let filter = CIFilter(name: pickerFilters[index])! 
    // 2 

    //filter.setValue(0.8, forKey: kCIInputIntensityKey) 
    let image = selectedImageArray[selectedIndex]       // 3 
    filter.setValue(image, forKey: kCIInputImageKey) 
    let result = filter.outputImage         // 4 this line and the below line are problematic 
    let cgImage = context.createCGImage(result!, from: (result?.extent)!) // 5 
    //  //load this image to imageview 
    imageView.image = UIImage(cgImage: cgImage!) 
    print("Filter Applied") 
} 

Le code me donne une erreur lorsque je clique sur le CollectionView pour sélectionner un filtre. Ce que je pensais que l'exception est à l'une de ces deux lignes

let result = filter.outputImage         // 4 this line and the below line are problematic 
    let cgImage = context.createCGImage(result!, from: (result?.extent)!) // 5 

si je retire ces deux lignes alors que le programme fonctionne bien, mais comme vous pouvez le voir filtre ne sera pas appliqué. S'il vous plaît laissez-moi savoir ce que je fais mal? Merci

Modifier Il dit quelque chose comme sélecteur non reconnu envoyé à l'instance .... Ci-dessous le détail de ce qui est produit dans la fenêtre de sortie lorsque exception occurrs.

Applying Filter.... 
2017-03-04 21:54:28.058 AVFrameWorkPractice[1223:31319] -[UIImage  extent]: unrecognized selector sent to instance 0x600000095720 
2017-03-04 21:54:28.145 AVFrameWorkPractice[1223:31319] *** Terminating  app due to uncaught exception 'NSInvalidArgumentException', reason:  '-[UIImage extent]: unrecognized selector sent to instance 0x600000095720' 
*** First throw call stack: 
(
    0 CoreFoundation      0x000000010d693d4b __exceptionPreprocess + 171 
1 libobjc.A.dylib      0x000000010d0f521e objc_exception_throw + 48 
2 CoreFoundation      0x000000010d703f04 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132 
3 CoreFoundation      0x000000010d619005 ___forwarding___ + 1013 
4 CoreFoundation      0x000000010d6a6a08 __forwarding_prep_1___ + 120 
5 CoreImage       0x000000010db9f9a4 -[CICMYKHalftone outputImage] + 336 
6 AVFrameWorkPractice     0x000000010bc49e7e _TFC19AVFrameWorkPractice20CustomEffectsCreator11applyFilterfT5indexSi_T_ + 1038 
7 AVFrameWorkPractice     0x000000010bc4962b _TFC19AVFrameWorkPractice20CustomEffectsCreator14collectionViewfTCSo16UICollectionView15didSelectItemAtV10Foundation9IndexPath_T_ + 1227 
8 AVFrameWorkPractice     0x000000010bc496a7 _TToFC19AVFrameWorkPractice20CustomEffectsCreator14collectionViewfTCSo16UICollectionView15didSelectItemAtV10Foundation9IndexPath_T_ + 87 
9 UIKit        0x000000010e9526e4 -[UICollectionView _selectItemAtIndexPath:animated:scrollPosition:notifyDelegate:] + 702 
10 UIKit        0x000000010e97d4d2 -[UICollectionView touchesEnded:withEvent:] + 649 
11 UIKit        0x000000010e230f6b forwardTouchMethod + 348 
12 UIKit        0x000000010e231034 -[UIResponder touchesEnded:withEvent:] + 49 
13 UIKit        0x000000010e230f6b forwardTouchMethod + 348 
14 UIKit        0x000000010e231034 -[UIResponder touchesEnded:withEvent:] + 49 
15 UIKit        0x000000010e54b304 _UIGestureEnvironmentSortAndSendDelayedTouches + 5645 
16 UIKit        0x000000010e545fcb _UIGestureEnvironmentUpdate + 1472 
17 UIKit        0x000000010e5459c3 -[UIGestureEnvironment _deliverEvent:toGestureRecognizers:usingBlock:] + 521 
18 UIKit        0x000000010e544ba6 -[UIGestureEnvironment _updateGesturesForEvent:window:] + 286 
19 UIKit        0x000000010e08ac1d -[UIWindow sendEvent:] + 3989 
20 UIKit        0x000000010e0379ab -[UIApplication sendEvent:] + 371 
21 UIKit        0x000000010e82472d __dispatchPreprocessedEventFromEventQueue + 3248 
22 UIKit        0x000000010e81d463 __handleEventQueue + 4879 
23 CoreFoundation      0x000000010d638761 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 
24 CoreFoundation      0x000000010d61d98c __CFRunLoopDoSources0 + 556 
25 CoreFoundation      0x000000010d61ce76 __CFRunLoopRun + 918 
26 CoreFoundation      0x000000010d61c884 CFRunLoopRunSpecific + 420 
27 GraphicsServices     0x0000000112734a6f GSEventRunModal + 161 
28 UIKit        0x000000010e019c68 UIApplicationMain + 159 
29 AVFrameWorkPractice     0x000000010bc4be4f main + 111 
30 libdyld.dylib      0x0000000112d2868d start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 

(LLDB)

+1

Tout point d'exclamation peut planter l'application. Si une méthode peut renvoyer 'nil', utilisez une liaison facultative. Si une méthode peut retourner 'nil' mais ne devrait pas, c'est une erreur de conception (développeur). – vadian

+0

Etes-vous sûr que 'selectedImageArray' contient' CIImage's? Le vidage d'exception semble avoir défini un 'UIImage' pour' kCIInputImageKey'. – OOPer

+0

Non, il contient UIImages. Devrais-je les convertir en CIImage? Je vais essayer ça. Merci. –

Répondre

0

Le problème était que je repassais UIImage au lieu de CIImage comme

let image = selectedImageArray[selectedIndex]       
filter.setValue(image, forKey: kCIInputImageKey) 

ce qui doit être fait a été

let image = selectedImageArray[selectedIndex]// 3 
let ciImage = CIImage(image: image) 
filter.setValue(ciImage, forKey: kCIInputImageKey) 

Ensuite, il fonctionne bien. Et aussi comme @ Ooper a dit

Nous devons passer l'étendue de l'image d'entrée au lieu de l'image de sortie car l'étendue de l'image de sortie peut être infinie.