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)
任何感嘆號都可能導致應用程序崩潰。如果一個方法可能會返回'nil'使用可選的綁定。如果一個方法可以返回'nil'但不應該,這是一個設計(開發人員)錯誤。 – vadian
你確定你的'selectedImageArray'包含'CIImage'嗎?異常轉儲似乎已經爲'kCIInputImageKey'設置了一個'UIImage'。 – OOPer
不,它包含UIImages。我應該將它們轉換爲CIImage嗎?將嘗試。謝謝。 –