Xcode相机:无法读取曝光偏差字典

28

在 Xcode 版本 12.0.1 中使用 UIImagePickerController 时,我最近遇到了这个错误。

[相机] 无法读取曝光补偿字典:错误域=NSCocoaErrorDomain Code=4864 "*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: data is NULL" UserInfo={NSDebugDescription=*** -[NSKeyedUnarchiver _initForReadingFromData:error:throwLegacyExceptions:]: data is NULL}

有没有其他人也遇到过这个错误?如何修复它?


2
我仍然有同样的问题。我的Xcode是12.4,iOS是14.4.2。 - yoppuyoppu
有人解决了这个问题吗?我实际上也遇到了同样的问题。 - Reims
9个回答

6
如果您将图像选择器自定义为imagePicker.allowsEditing = true,则必须使用以下方式获取图像:
if let pickedImage = info[UIImagePickerController.InfoKey.editedImage] as? UIImage {
    capturedImage = pickedImage    
}

如果您使用imagePicker.allowsEditing = false,则使用以下内容来选择图片:
if let pickedImage = info[UIImagePickerController.InfoKey.originalImage] as? UIImage {
    capturedImage = pickedImage        
}

如果您不遵循这个组合,您可能会收到此错误。


很遗憾,这对我不起作用(在iOS 14.8.1 Xcode 13.2.1,iPhone 12 Pro上进行了测试)。也许只是一个无害的警告或者是苹果的一个bug? - CyberMew

2
在我的情况下,我尝试使用图像数据并与文件同步时遇到了这个错误。在Info.plist中添加此权限使所有差异消失,并使该错误消失: <key>LSSupportsOpeningDocumentsInPlace</key> <true/>

这解决了我的问题。谢谢。 - user139816

1
我成功解决了问题。实际上,它与 react-native-image-crop-picker 没有直接关系。问题在于我使用 react-native-actionsheet 给用户提供打开相机或图库的选项。当我打开 react-native-actionsheet 并按下其中一个选项时,相机会叠加在 react-native-actionsheet (模态) 上,这会生成冲突,因为显然在 IOS 中,一个 Modal 不可能重叠在另一个 Modal 上。
因此,为了解决问题,我定义了一个计时器,以便在打开相机之前关闭模态。

1
我遇到了同样的问题。我导入了AVKit而不是AVFoundation,并尝试在本机录制视图中显示视频。这给了我一个异常,告诉我要将NSMicrophoneUsageDescription添加到info.plist文件中,之后,我能够在自定义视图中显示实时视频。
所以我认为问题在于iOS 14对权限非常挑剔,可能在未能在本机视图中显示正确的异常时出现了问题。
无论如何,这对我有效:
import AVKit
import MobileCoreServices

@IBOutlet weak var videoViewContainer: UIView!

private let imagePickerController = UIImagePickerController()

override func viewDidLoad() {
    super.viewDidLoad()
    initCameraView()
}

func initCameraView() {
    // Device setup
    imagePickerController.delegate = self
    imagePickerController.sourceType = .camera
    imagePickerController.mediaTypes = [kUTTypeMovie as String]
    imagePickerController.cameraCaptureMode = .video
    imagePickerController.cameraDevice = .rear
    
    // UI setup
    addChild(imagePickerController)
    videoViewContainer.addSubview(imagePickerController.view)
    imagePickerController.view.frame = videoViewContainer.bounds
    imagePickerController.allowsEditing = false
    imagePickerController.showsCameraControls = false
    imagePickerController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
}

然后在info.plist文件中添加NSMicrophoneUsageDescription的说明:

希望这对你也有用!


0
如果你和我一样,也收到了这个第二条信息:

[access] This app has crashed because it attempted to access privacy-sensitive data without a usage description.  The app's Info.plist must contain an NSCameraUsageDescription key with a string value explaining to the user how the app uses this data.

然后您需要将此添加到您的info.plist字典中:

<key>NSCameraUsageDescription</key>
    <string>so you can choose a photo or take a picture for object detection</string>

它为我解决了这个问题


0
在我的情况下,我缺少了一个 Info.plist 键,即 NSCameraUsageDescription。 您应该输入使用相机的目的作为描述。 这为我解决了崩溃问题。 此外,如果您不提供目的,您的应用程序很可能会被拒绝。

0

当我尝试从无法复制的URL复制时,出现了这个错误。这个URL来自于UIImagePickerControllerDelegate中的mediaURL

基本上,我所做的就是使用UISaveVideoAtPathToSavedPhotosAlbum

就像这个例子⤵️中所示:

if UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(url.absoluteString) {
            UISaveVideoAtPathToSavedPhotosAlbum(url.absoluteString, self, #selector(self.didSaveVideo), nil)
        } else {
            return /* do something*/
        }

@objc private func didSaveVideo(videoPath: String, error: NSError, contextInfo: Any) {}

0

我在使用Xcode 12和iOS 14时遇到了相同的错误。

但是在我的情况下,我在之前使用了ActionSheet来选择相机或照片库。所以我改为在关闭ActionSheet后立即打开相机,现在它可以正常工作。

希望这对你的问题有所帮助。

    
    enum MediaOptions: Int {
        case Photos
        case Camera
    }

    func selectImage(mediaType: MediaOptions) {
        self.mediaOption = mediaType
        let iPicker = UIImagePickerController()
        iPicker.delegate = self
        iPicker.allowsEditing = false
        if mediaType == .Camera {
            if UIImagePickerController.isSourceTypeAvailable(.camera) {
                iPicker.sourceType = .camera
                iPicker.allowsEditing = true
            }
        } else {
            iPicker.sourceType = .photoLibrary
        }
        self.present(iPicker, animated: true, completion: nil)
        self.imagePicker = iPicker
    }

    func choosePhoto() {
        let actionSheet = UIAlertController(title: "Choose", message: "", preferredStyle: .actionSheet)
        
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            actionSheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (action) -> Void in
                actionSheet.dismiss(animated: true) {
                    self.selectImage(mediaType: .Camera) // Just moved here - inside the dismiss callback
                }
            }))
        }
        if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
            actionSheet.addAction(UIAlertAction(title: "Photo Library", style: .default, handler: { (action) -> Void in
                self.selectImage(mediaType: .Photos)
            }))
        }
        
        actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        
        self.present(actionSheet, animated: true, completion: nil)
    }

0

当imagePicker的source type为相机时,我在Xcode 12和iOS 14中发现了相同的错误。

但是应用程序正常工作,我可以使用相机拍照并将其放入我的集合视图单元格中。因此,可能是Xcode 12上的某些问题吧。

    @objc func addPerson() {
        let picker = UIImagePickerController()
        if UIImagePickerController.isSourceTypeAvailable(.camera) {
            picker.sourceType = .camera
        } else {
            fatalError("Camera is not available, please use real device.")
        }
        picker.allowsEditing = true
        picker.delegate = self
        present(picker, animated: true)
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接