UIImagePickerController 只能在主线程中使用 |应用崩溃

UIImagePickerController must be used from main thread only | app crash

大家好,我正在 iOS14 中实施 UIImagePickerController。当用户选择相机时,如果用户同意使用相机,我的应用程序会崩溃,并在 xcode

上以紫色返回此错误
- [UIImagePickerController init] must be used from main thread only

我该如何解决这个问题? 谢谢大家的建议

private func cameraAuth(auth sourceType: UIImagePickerController.SourceType) {
    switch AVCaptureDevice.authorizationStatus(for: .video) {
    case .denied: imagePickerAuthDenied()
    case .restricted: imagePickerAuthRestricted()
    case .authorized: showImagePicker(sourceType: sourceType)
   
    case .notDetermined:

        AVCaptureDevice.requestAccess(for: .video) { (success) in
            if success { self.showImagePicker(sourceType: sourceType) }
            else { self.imagePickerAuthDenied() }
        }
        
    default : print("Default Switch Camera Auth")
    }
}

private func showImagePicker(sourceType: UIImagePickerController.SourceType) {
   
    let imagePickerController = UIImagePickerController()
    imagePickerController.delegate = self
    imagePickerController.sourceType = sourceType
    imagePickerController.allowsEditing = true
    present(imagePickerController, animated: true, completion: nil)
}

您需要将代码包装在 authorizationStatus/AVCaptureDevice.requestAccess(for: .video) { (success) in in DispatchQueue.main.async

AVCaptureDevice.authorizationStatus(for: .video) {
   DispatchQueue.main.async {
     ///
   }
}

AVCaptureDevice.requestAccess(for: .video) { (success) in 
   DispatchQueue.main.async {
     ///
   }
}