选择图像时未捕获的异常“NSInvalidArgumentException”
Posted
技术标签:
【中文标题】选择图像时未捕获的异常“NSInvalidArgumentException”【英文标题】:uncaught exception 'NSInvalidArgumentException' when selecting an Image 【发布时间】:2021-01-28 14:14:11 【问题描述】:在我当前的应用项目中,我可以选择一张图片。该功能已经运行了一段时间。但是,当我单击图像选择按钮时,现在出现以下错误。我试图找出错误的根源,但我似乎找不到它。
我怀疑该问题与图像选择功能无关,而是与应用程序中的其他内容有关。当我将相同的代码带入一个新的应用程序项目时,图像选择功能可以正常工作。请帮我指出正确的方向。
2021-01-28 08:43:54.883769-0500 Social Squash[11718:144431] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: NSColor)'
*** First throw call stack:
(
0 CoreFoundation 0x00007fff20421af6 __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007fff20177e78 objc_exception_throw + 48
2 CoreFoundation 0x00007fff2049e77f _CFThrowFormattedException + 194
3 CoreFoundation 0x00007fff204aa035 -[__NSDictionaryM setObject:forKey:].cold.3 + 0
4 CoreFoundation 0x00007fff2048e134 -[__NSDictionaryM setObject:forKey:] + 922
5 UIKitCore 0x00007fff24204dd4 -[_UIAppearanceRecorder _recordInvocation:withClassName:containerClassNames:traitCollection:selectorString:forRemoteProcess:] + 2705
6 UIKitCore 0x00007fff241ff355 __54+[_UIAppearance _recordersExcludingSource:withWindow:]_block_invoke + 891
7 CoreFoundation 0x00007fff2037b9e3 __NSDICTIONARY_IS_CALLING_OUT_TO_A_BLOCK__ + 7
8 CoreFoundation 0x00007fff2048f06f -[__NSDictionaryM enumerateKeysAndObjectsWithOptions:usingBlock:] + 225
9 UIKitCore 0x00007fff241fefc8 +[_UIAppearance _recordersExcludingSource:withWindow:] + 131
10 UIKitCore 0x00007fff24ad18b9 UIViewServiceCurrentAppearanceSerializedRepresentations + 84
11 UIKitCore 0x00007fff24aa3908 +[_UIRemoteViewController _requestViewController:traitCollection:fromServiceWithBundleIdentifier:service:connectionHandler:] + 353
12 UIKitCore 0x00007fff24aa3773 +[_UIRemoteViewController requestViewControllerWithService:traitCollection:connectionHandler:] + 79
13 UIKitCore 0x00007fff24087917 __146-[NSExtension(UIViewControllerAdditions) _instantiateViewControllerWithInputItems:asAccessory:traitCollection:listenerEndpoint:connectionHandler:]_block_invoke_2 + 554
14 libdispatch.dylib 0x000000010c11d7ec _dispatch_call_block_and_release + 12
15 libdispatch.dylib 0x000000010c11e9c8 _dispatch_client_callout + 8
16 libdispatch.dylib 0x000000010c12ce75 _dispatch_main_queue_callback_4CF + 1152
17 CoreFoundation 0x00007fff2038fdbb __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
18 CoreFoundation 0x00007fff2038a63e __CFRunLoopRun + 2685
19 CoreFoundation 0x00007fff203896d6 CFRunLoopRunSpecific + 567
20 GraphicsServices 0x00007fff2c257db3 GSEventRunModal + 139
21 UIKitCore 0x00007fff24696cf7 -[UIApplication _run] + 912
22 UIKitCore 0x00007fff2469bba8 UIApplicationMain + 101
23 Social Squash 0x0000000104c01d1b main + 75
24 libdyld.dylib 0x00007fff2025a3e9 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSDictionaryM setObject:forKey:]: object cannot be nil (key: NSColor)'
terminating with uncaught exception of type NSException
CoreSimulator 732.18.6 - Device: iPhone 12 Pro Max (FCAFC159-47E8-4687-A7CB-9A6300821E09) - Runtime: ios 14.4 (18D46) - DeviceType: iPhone 12 Pro Max
我的代码如下:
import SwiftUI
struct ImagePickerExampleView: View
@State var showImagePicker: Bool = false
@State var image: UIImage?
var body: some View
VStack
if image != nil
Image(uiImage: image!)
.resizable()
.aspectRatio(contentMode: .fit)
Button("Pick image")
self.showImagePicker.toggle()
.sheet(isPresented: $showImagePicker)
ImagePickerView(sourceType: .photoLibrary) image in
self.image = image
struct ImagePickerExampleView_Previews: PreviewProvider
static var previews: some View
ImagePickerExampleView()
这是 UIController 的代码
public struct ImagePickerView: UIViewControllerRepresentable
private let sourceType: UIImagePickerController.SourceType
private let onImagePicked: (UIImage) -> Void
@Environment(\.presentationMode) private var presentationMode
public init(sourceType: UIImagePickerController.SourceType, onImagePicked: @escaping (UIImage) -> Void)
self.sourceType = sourceType
self.onImagePicked = onImagePicked
public func makeUIViewController(context: Context) -> UIImagePickerController
let picker = UIImagePickerController()
picker.sourceType = self.sourceType
picker.delegate = context.coordinator
return picker
public func updateUIViewController(_ uiViewController: UIImagePickerController, context: Context)
public func makeCoordinator() -> Coordinator
Coordinator(
onDismiss: self.presentationMode.wrappedValue.dismiss() ,
onImagePicked: self.onImagePicked
)
final public class Coordinator: NSObject, UINavigationControllerDelegate, UIImagePickerControllerDelegate
private let onDismiss: () -> Void
private let onImagePicked: (UIImage) -> Void
init(onDismiss: @escaping () -> Void, onImagePicked: @escaping (UIImage) -> Void)
self.onDismiss = onDismiss
self.onImagePicked = onImagePicked
public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey: Any])
if let image = info[.originalImage] as? UIImage
self.onImagePicked(image)
self.onDismiss()
public func imagePickerControllerDidCancel(_: UIImagePickerController)
self.onDismiss()
【问题讨论】:
您在某处使用NSAttributeString
吗?因为NSColor
是NSAttributedString.Key.foregroundColor
的rawValue
。您正在设置无效的颜色。也许不是直接的,但也许是UIAppearance
(可能是对NSAttributeString
的隐藏调用,但确实是UIAppearance
调用)。这就是罪魁祸首。
这看起来不像这段代码中的问题,在你修改外观的地方显示一个。
谢谢@Larme。问题出在设置 NavigationBar 文本标题的 init() 语句中。我有以下声明: UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : dim.navbar.fontColor]。我错过了 .uiColor() 和 .fontColor 的结尾
【参考方案1】:
问题出在设置 NavigationBar 文本标题的 init() 语句中。我有以下声明:
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : dim.navbar.fontColor]
我错过了 .uiColor() 和 .fontColor 的结尾
UINavigationBar.appearance().titleTextAttributes = [.foregroundColor : dim.navbar.fontColor.uiColor()]
【讨论】:
以上是关于选择图像时未捕获的异常“NSInvalidArgumentException”的主要内容,如果未能解决你的问题,请参考以下文章
DbUpdateException- 在我捕获所有异常时未处理重复的键错误