ios - 相机访问请求失败 - 但仅在第一次访问相机时失败 Swift 3

标签 ios swift camera

此 iOS 应用程序使用相机将图像放置在主详细信息应用程序的详细信息 View 中。当安装应用程序并第一次请求拍照操作时,应用程序崩溃。从代码中可以看出,请求访问并向用户显示请求访问的警报。一旦授予完成处理程序报告成功并且应用程序崩溃。如果我重新启动应用程序(而不重新安装),相机将按预期运行并继续这样做。从 iPhone 中删除应用程序并重新安装总是会产生完全相同的结果。

控制台输出: AVAuthorizationStatus 未确定。
用户已授予权限。
libc++abi.dylib:以 NSException 类型的未捕获异常终止

如有任何帮助,我们将不胜感激。 iOS 10、Xcode 8、Swift 3

编辑:它在 AVCaptureRequest 中崩溃。如果我注释掉 AVCaptureRequest,应用程序不会崩溃(当然没有照片)。

@IBAction func takeDrugPhoto(_ sender: UIButton) {


    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {

        let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)

        switch authStatus {

        case AVAuthorizationStatus.authorized:
            print("AVAuthorizationStatus is Authorized")

......这里有一堆代码......

        case AVAuthorizationStatus.denied:
            print("AVAuthorizationStatus is Denied")

.....这里有一些代码......

        case AVAuthorizationStatus.notDetermined:
            print("AVAuthorizationStatus is NotDetermined")


            AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeVideo, completionHandler: { (success) in

                if success {
                    print("The user granted permission")

                } else {
                    print("put up an alert telling the user the camera is not available")

                    DispatchQueue.main.async(execute: { () -> Void in
                        let ac = UIAlertController(title: "Camera Error", message: "For some reason, the camera in this device is not accepting your authorization. Check with your device supplier.", preferredStyle: .alert)
                        ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
                        self.present(ac, animated: true, completion: nil)
                    })//back on main queue block
                }//if success

            })//requestAccessForMediaType block


        }//switch

    } else {//if device has a camera

        let ac = UIAlertController(title: "Source not available.", message: "The camera is not available.", preferredStyle: .alert)
        ac.addAction(UIAlertAction(title: "Ok", style: .default, handler: nil))
        present(ac, animated: true, completion: nil)

    }//if camera is no else

}//takeDrugPhoto

从跟踪中:

最佳答案

当您通过 AVCaptureDevice.requestAccess 请求寻求用户许可时,iOS 会显示一个警报对话框,其中包含您在 info.plist 中创建的消息。当用户授予权限时,iOS 会认为该应用程序就像从后台返回一样(即使它并没有消失)。那是我的问题。作为安全性的一部分,我构建了一个登录序列,每当应用程序从后台返回时就会激活。我已经禁用了该接口(interface)进行测试 - 因此失败了。我希望其他人会发现这很有用。

关于ios - 相机访问请求失败 - 但仅在第一次访问相机时失败 Swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44276064/

相关文章:

ios - 当用户使用 MapKit、Swift 点击 map 时如何执行操作

0x00000040 处的 Android 致命信号 11 (SIGSEGV)(代码=1)错误

java - 从相机拍摄照片在 Android 11 中不起作用

ios - Facebook iOS native 应用程序中的权限对话框

ios - UICollectionView - 选择第一个单元格作为 View 将出现

swift - 如何在 SKCamera 的 View 中识别特定节点?

ios - 如何将现有对象添加到 Parse.com Databrowser 中的关系中

ios - 从相机拍摄的 iphone 图像自动旋转 -90 度

iphone - 使用 pushViewController 时出现 SIGABRT 错误

ios - 如何在 Objective-C 中检查一个对象是否为 nil?