我正在尝试使用UIImagePickerController来允许从图书馆或通过相机拍照来选择照片。
我按照网站(https://makeapppie.com/2016/06/28/how-to-use-uiimagepickercontroller-for-a-camera-and-photo-library-in-swift-3-0/)上的步骤操作,并将其用于照片库,但每当我尝试从我的应用程序调用相机时,都会出现错误“线程1:信号SIGABRT”。
这是我用来调用相机的代码:
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
据我了解,模拟器内部会出现SIGABRT错误。然而,当我在我的iPhone 7上尝试它时,我希望它能够正常工作并且它会产生同样的错误。
我已将“隐私 - 相机使用说明”添加到Info.plist文件中。
任何想法我做错了什么?
答案 0 :(得分:2)
这是我使用/选择
的完整代码// MARK: Camera App
func openCameraApp() {
if UIImagePickerController.availableCaptureModes(for: .rear) != nil {
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,
animated: true,
completion: nil)
} else {
noCamera()
}
}
func noCamera(){
let alertVC = UIAlertController(
title: "No Camera",
message: "Sorry, this device has no camera",
preferredStyle: .alert)
let okAction = UIAlertAction(
title: "OK",
style:.default,
handler: nil)
alertVC.addAction(okAction)
present(
alertVC,
animated: true,
completion: nil)
}
// MARK: Photos Albums
func showImagePicker() {
picker.allowsEditing = false
picker.sourceType = .photoLibrary
//picker.modalPresentationStyle = .Popover
present(picker,
animated: true,
completion: nil)
picker.popoverPresentationController?.sourceView = self.view
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage
image = chosenImage
self.performSegue(withIdentifier: "ShowEditView", sender: self)
dismiss(animated: true, completion: nil)
}
func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
dismiss(animated: false, completion: nil)
}
// MARK: Seque to EditViewController
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ShowEditView" {
if let vc = segue.destination as? EditViewController {
vc.image = image
//vc.image = images[0]
}
}
}
忽略两条已注释掉的行 - 这些是我测试的东西。此代码适用于所有设备,iOS 9 +,所有方向(但请记住iPhone始终显示为肖像),我在模拟器(它没有摄像头)和物理设备方面都没有任何问题。
感兴趣的是可能导致问题的一件事(不确定它是否会抛出SIGABRT) - 我正在检查后置摄像头并在不存在的情况下发出警报。 (虽然没有检查前置摄像头。我甚至不确定除了iPod touch之外什么都没有前置摄像头。)
另外,不要忘记在iOS 10的info.plist中添加两件事:
<key>NSCameraUsageDescription</key>
<string>Used to capture new image for photo effect</string>
<key>NSPhotoLibraryUsageDescription</key>
<string>Used to select an image for photo effect</string>
您可以在描述标签中添加任何内容。如果没有这些,应用程序将在iOS 10中关闭,Apple将拒绝您的提交。 Here's指向更多详情的链接。