此iOS應用程序使用相機將圖像放置在主細部應用程序的詳細視圖中。當應用程序安裝並且第一次請求拍照操作時,應用程序崩潰。如代碼中所示,請求訪問並向用戶提供請求訪問的警報。一旦授予完成處理程序報告成功和應用程序崩潰。如果我重新啓動應用程序(無需重新安裝),相機將按預期執行並繼續執行此操作。從iPhone上刪除應用程序並重新安裝總是產生完全相同的結果。相機訪問請求失敗 - 但僅在第一次訪問相機時Swift 3
控制檯輸出: 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
從跟蹤:
哪部分代碼會崩潰?並請提供上面添加的異常 –
信息的堆棧跟蹤。 – user2698617
您是否在Info.plist中爲'Privacy - Camera Usage Description'鍵添加了值? –