Ios UIIMAGEPICKER控制器投掷SIGABRT
我正在使用UIImagePickerController进行实验,以允许从库中选择照片或使用相机拍照 我在一个网站()上遵循了这些步骤,并在照片库中实现了这一点,但每当我尝试从我的应用程序中调用相机时,就会出现错误“线程1:信号SIGABRT” 这是我用来调用相机的代码:Ios UIIMAGEPICKER控制器投掷SIGABRT,ios,iphone,swift,camera,sigabrt,Ios,Iphone,Swift,Camera,Sigabrt,我正在使用UIImagePickerController进行实验,以允许从库中选择照片或使用相机拍照 我在一个网站()上遵循了这些步骤,并在照片库中实现了这一点,但每当我尝试从我的应用程序中调用相机时,就会出现错误“线程1:信号SIGABRT” 这是我用来调用相机的代码: picker.allowsEditing = false picker.sourceType = UIImagePickerControllerSourceType.camera picker.cameraCaptureMod
picker.allowsEditing = false
picker.sourceType = UIImagePickerControllerSourceType.camera
picker.cameraCaptureMode = .photo
picker.modalPresentationStyle = .fullScreen
present(picker,animated: true,completion: nil)
据我所知,SIGABRT错误将在模拟器内出现。然而,当我在我的iPhone7上试用它时,我希望它能工作,但它给出了相同的错误
我已将“隐私-摄像头使用说明”添加到Info.plist文件中
你知道我做错了什么吗?这是我使用/选择的完整代码
// 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)——我正在检查后置摄像头,如果它不存在,就会发出警报。(但没有检查前置摄像头。我甚至不确定除了iPodtouch以外的任何东西是否没有前置摄像头。)
此外,别忘了在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>
NSCameraUsageDescription
用于捕获新图像以获得照片效果
NSPhotoLibraryUsageDescription
用于选择照片效果的图像
您可以在描述标签中放入任何需要的内容。如果没有这些,应用程序将在iOS 10中关闭,苹果将拒绝您的提交。更多详细信息的链接。这是我使用/选择的完整代码
// 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)——我正在检查后置摄像头,如果它不存在,就会发出警报。(但没有检查前置摄像头。我甚至不确定除了iPodtouch以外的任何东西是否没有前置摄像头。)
此外,别忘了在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>
NSCameraUsageDescription
用于捕获新图像以获得照片效果
NSPhotoLibraryUsageDescription
用于选择照片效果的图像
您可以在描述标签中放入任何需要的内容。如果没有这些,应用程序将在iOS 10中关闭,苹果将拒绝您的提交。更多详细信息的链接。谢谢您的回答。对不起,我原来的问题有点模棱两可。我希望在模拟器中得到错误,但在我的iPhone上没有。我在这两个问题上都遇到了相同的错误。我将用完整的代码编辑我的答案。如果没有帮助,请告诉我,我会删除它。谢谢。@AlanSpark-您的问题应该通过更新您的
info.plist
文件来解决,就像@dfd建议的那样。如果需要更简单的方法,请复制并粘贴键,然后在将新行插入info.plist
文件时,在值列中手动输入值。例如,创建新行并将NSCameraUsageDescription
粘贴到“Key”列中。Xcode会自动更新它,比如说“隐私-摄像头访问”,然后在“值”栏中输入描述。我想我刚才描述的内容实际上并不“简单”,但对于那些不习惯手动编辑其info.plist
文件源代码的人来说,它更方便。谢谢你们的帮助。我怀疑我在做傻事,结果就是这样。我做的每件事都是正确的,但问题是,不知何故,我连接了一个不存在的幻影IBAction。正是这一点导致了这场车祸,而不是那架被证明是骗人的照相机。谢谢你的回答。对不起,我原来的问题有点模棱两可。我希望在模拟器中得到错误,但在我的iPhone上没有。我在这两个问题上都遇到了相同的错误。我将用完整的代码编辑我的答案。如果没有帮助,请告诉我,我会删除它。谢谢。@AlanSpark-您的问题应该通过更新您的info.plist
文件来解决,就像@dfd建议的那样。如果需要更简单的方法,请复制并粘贴键,然后在将新行插入info.plist
文件时,在值列中手动输入值。例如,创建新行并将NSCameraUsageDescription
粘贴到“Key”列中。Xcode会自动更新它,比如说“隐私-摄像头访问”,然后在“值”栏中输入描述。我想我刚才描述的内容实际上并不“简单”,但对于那些不习惯手动编辑其info.plist
文件源代码的人来说,它更方便。谢谢你们的帮助。我怀疑我在做傻事,结果就是这样。我做的每件事都是正确的,但问题是,不知何故,我连接了一个不存在的幻影IBAction。正是这一点导致了这场车祸,而不是摄像机造成的误会。