gpt4 book ai didi

ios - 应用过滤器时出现 NSInvalidArgumentException

转载 作者:行者123 更新时间:2023-11-30 12:38:15 25 4
gpt4 key购买 nike

我正在编写代码以向图片添加滤镜。这是代码

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 选择过滤器时,代码出现错误。我认为异常(exception)是在这两行中的任何一行

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

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

Edit It says something like unrecognized selector sent to instance .... Below is the detail of what is produced in the output window when 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)

最佳答案

问题是我传递的是 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 所说

We must pass extent of input image instead of output Image as extent of output image can be infinite.

关于ios - 应用过滤器时出现 NSInvalidArgumentException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42599344/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com