2017-03-04 75 views
0

我在编写代码以向图片添加过滤器。这里是代码应用过滤器时出现NSInvalidArgumentException

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") 
} 

当我点击collectionView选择一个过滤器时,代码给我一个错误。我想通的是,例外的是在任何这两条线

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

,如果我删除这两条线,则程序工作正常,但你可以看到过滤器将不会被应用。请让我知道我做错了什么? 感谢

编辑 它说像无法识别的选择发送到实例.... 下面是当异常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

任何感叹号都可能导致应用程序崩溃。如果一个方法可能会返回'nil'使用可选的绑定。如果一个方法可以返回'nil'但不应该,这是一个设计(开发人员)错误。 – vadian

+0

你确定你的'selectedImageArray'包含'CIImage'吗?异常转储似乎已经为'kCIInputImageKey'设置了一个'UIImage'。 – OOPer

+0

不,它包含UIImages。我应该将它们转换为CIImage吗?将尝试。谢谢。 –

回答

0

的问题是,我是路过的UIImage代替CIImage像

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

必须做什么是

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

然后,它的工作原理精细。还有@Ooper说

我们必须通过输入图像的范围而不是输出图像作为输出图像的范围可以是无限的。