我正在编写代码来为图片添加过滤器。这是代码
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
如果我删除这两行然后程序工作正常,但你可以看到过滤器将不会应用。请让我知道我做错了什么? 感谢
编辑 它说像无法识别的选择器发送到实例.... 下面是发生异常时在输出窗口中生成的内容的详细信息。
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)
答案 0 :(得分: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说的
我们必须传递输入图像的范围而不是输出图像,因为输出图像的范围可以是无限的。