主线程检查器报错:UIImagePickerController后台线程调用UI API
问题:UIImagePickerController触发主线程检查器报错,后台线程调用UI API
点击导航栏按钮打开UIImagePickerController时,调试器弹出主线程检查器错误:UI API在后台线程被调用-[UIImagePickerController didDisplayPhotoPickerSourceType:],找不到该方法的官方文档。已确保在主线程present图片选择器,但错误依然出现。
相关代码片段
override func viewDidLoad() { super.viewDidLoad() collectionView.backgroundColor = .blue register() setUp() collectionView.reloadData() } private func setUp() { navigationController?.isNavigationBarHidden = false let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout layout.minimumInteritemSpacing = 2 layout.minimumLineSpacing = 2 collectionView.collectionViewLayout = layout navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNewPerson)) } @objc private func addNewPerson() { DispatchQueue.main.async { [weak self] in let picker = UIImagePickerController() picker.allowsEditing = true picker.delegate = self self?.present(picker, animated: false) } }
调试日志
Main Thread Checker: UI API called on a background thread: -[UIImagePickerController didDisplayPhotoPickerSourceType:] PID: 85527, TID: 2803975, Thread name: (none), Queue name: com.apple.NSXPCConnection.user.anonymous.85796, QoS: 0 Backtrace: 4 PhotosUI 0x00000001ad0380c8 -[PUPhotoPickerExtensionHostContext didDisplayPhotoPickerSourceType:] + 96 5 Foundation 0x0000000180fb6c10 __NSXPCCONNECTION_IS_CALLING_OUT_TO_EXPORTED_OBJECT_S1__ + 12 6 Foundation 0x0000000180fb59e0 -[NSXPCConnection _decodeAndInvokeMessageWithEvent:reply:flags:] + 1664 7 Foundation 0x0000000180fb79ec message_handler_message + 84 8 Foundation 0x0000000180fb6f80 message_handler + 148 9 libxpc.dylib 0x00000001800cca0c _xpc_connection_call_event_handler + 68 10 libxpc.dylib 0x00000001800ce10c _xpc_connection_mach_event + 1048 11 libdispatch.dylib 0x0000000105b3f904 _dispatch_client_callout4 + 12 12 libdispatch.dylib 0x0000000105b42a90 _dispatch_mach_msg_invoke + 672 13 libdispatch.dylib 0x0000000105b2d75c _dispatch_lane_serial_drain + 324 14 libdispatch.dylib 0x0000000105b43d50 _dispatch_mach_invoke + 504 15 libdispatch.dylib 0x0000000105b2d75c _dispatch_lane_serial_drain + 324 16 libdispatch.dylib 0x0000000105b2e708 _dispatch_lane_invoke + 420 17 libdispatch.dylib 0x0000000105b3b3ac _dispatch_root_queue_drain_deferred_wlh + 324 18 libdispatch.dylib 0x0000000105b3a918 _dispatch_workloop_worker_thread + 732 19 libsystem_pthread.dylib 0x0000000247927b74 _pthread_wqthread + 284 20 libsystem_pthread.dylib 0x0000000247926934 start_wqthread + 8 Main Thread Checker: UI API called on a background thread: -[UIImagePickerController didDisplayPhotoPickerSourceType:] PID: 85527, TID: 2803975, Thread name: (none), Queue name: com.apple.NSXPCConnection.user.anonymous.85796, QoS: 0 Backtrace: 4 PhotosUI 0x00000001ad0380c8 -[PUPhotoPickerExtensionHostContext didDisplayPhotoPickerSourceType:] + 96 5 Foundation 0x0000000180fb6c10 __NSXPCCONNECTION_IS_CALLING_OUT_TO_EXPORTED_OBJECT_S1__ + 12 6 Foundation 0x0000000180fb59e0 -[NSXPCConnection _decodeAndInvokeMessageWithEvent:reply:flags:] + 1664 7 Foundation 0x0000000180fb79ec message_handler_message + 84 8 Foundation 0x0000000180fb6f80 message_handler + 148 9 libxpc.dylib 0x00000001800cca0c _xpc_connection_call_event_handler + 68 10 libxpc.dylib 0x00000001800ce10c _xpc_connection_mach_event + 1048 11 libdispatch.dylib 0x0000000105b3f904 _dispatch_client_callout4 + 12 12 libdispatch.dylib 0x0000000105b42a90 _dispatch_mach_msg_invoke + 672 13 libdispatch.dylib 0x0000000105b2d75c _dispatch_lane_serial_drain + 324 14 libdispatch.dylib 0x0000000105b43d50 _dispatch_mach_invoke + 504 15 libdispatch.dylib 0x0000000105b2d75c _dispatch_lane_serial_drain + 324 16 libdispatch.dylib 0x0000000105b2e708 _dispatch_lane_invoke + 420 17 libdispatch.dylib 0x0000000105b3b3ac _dispatch_root_queue_drain_deferred_wlh + 324 18 libdispatch.dylib 0x0000000105b3a918 _dispatch_workloop_worker_thread + 732 19 libsystem_pthread.dylib 0x0000000247927b74 _pthread_wqthread + 284 20 libsystem_pthread.dylib 0x0000000247926934 start_wqthread + 8
解决方案
这个错误属于Apple PhotosUI框架内部问题,和你的代码无关。从堆栈日志能看到,错误来自PUPhotoPickerExtensionHostContext的内部NSXPC通信回调,该回调被分发到后台线程,触发了主线程检查器警告。
可以按以下方式处理:
- 直接忽略警告:该警告不会影响App功能,只是框架内部线程调用不符合规范,发布版本中主线程检查器默认关闭,不会出现这个提示。
- 临时禁用主线程检查器:开发时如果不想看到这个警告,可在Scheme设置中关闭"Main Thread Checker"(路径:Edit Scheme -> Run -> Diagnostics -> 取消勾选Main Thread Checker)。
- 改用PHPickerViewController:如果想彻底避免这类问题,推荐使用iOS 14+推出的
PHPickerViewController,它是UIImagePickerController的现代替代方案,设计更合理,能避免很多旧框架的内部问题。示例代码如下:
import PhotosUI @objc private func addNewPerson() { DispatchQueue.main.async { [weak self] in var config = PHPickerConfiguration() config.filter = .images config.selectionLimit = 1 config.allowsEditing = true let picker = PHPickerViewController(configuration: config) picker.delegate = self self?.present(picker, animated: true) } } // 实现PHPickerViewControllerDelegate extension YourViewController: PHPickerViewControllerDelegate { func picker(_ picker: PHPickerViewController, didFinishPicking results: [PHPickerResult]) { picker.dismiss(animated: true) guard let result = results.first else { return } if result.itemProvider.canLoadObject(ofClass: UIImage.self) { result.itemProvider.loadObject(ofClass: UIImage.self) { image, error in DispatchQueue.main.async { // 处理选中的图片 if let selectedImage = image as? UIImage { // 执行你的业务逻辑 } } } } } }
内容的提问来源于stack exchange,提问作者brunshte
相关产品推荐
相关产品推荐

