在iOS SWIFT3.1中,我试图访问相机,就像我在其他应用程序中成功地做的那样。然而,在这个特定的应用程序中,它总是在self.present(imagePicker, animated: true, completion: nil)行上崩溃。控制台中的消息是Message from debugger: Terminated due to signal 9。这通常表示与内存相关的错误吗?
@IBAction func onChooseImageClick(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.savedPhotosAlbum){
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
//Create the AlertController and add Its action like button in Actionsheet
let actionSheetControllerForImage: UIAlertController = UIAlertController(title: "Please select", message: "Option to select", preferredStyle: .actionSheet)
let cancelActionButton: UIAlertAction = UIAlertAction(title: "Cancel", style: .cancel) { action -> Void in
print("Cancel")
}
actionSheetControllerForImage.addAction(cancelActionButton)
let cameraActionButton: UIAlertAction = UIAlertAction(title: "Camera", style: .default)
{ action -> Void in
print("Camera")
if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {
imagePicker.sourceType = UIImagePickerControllerSourceType.camera
let mediaTypes:[String] = [kUTTypeImage as String]
imagePicker.mediaTypes = mediaTypes
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
} else {
let alertController = UIAlertController(title: "error", message: "Camera not found!", preferredStyle: .alert)
let OKAction = UIAlertAction(title: "OK", style: .cancel) { action in
// ...
}
alertController.addAction(OKAction)
self.present(alertController, animated: true)
}
}
actionSheetControllerForImage.addAction(cameraActionButton)
let galleryActionButton: UIAlertAction = UIAlertAction(title: "Image Gallery", style: .default)
{ action -> Void in
imagePicker.sourceType = UIImagePickerControllerSourceType.savedPhotosAlbum;
imagePicker.allowsEditing = false
self.present(imagePicker, animated: true, completion: nil)
}
actionSheetControllerForImage.popoverPresentationController?.sourceView = self.view
actionSheetControllerForImage.addAction(galleryActionButton)
===> self.present(actionSheetControllerForImage, animated: true, completion: nil)
}
}发布于 2017-04-25 01:10:40
您需要在您的plist文件中的字符串,解释为什么您的应用程序需要许可使用相机。在iOS 10 - Changes in asking permissions of Camera, microphone and Photo Library causing application to crash的问题中有一个关于这些字符串的很好的总结,您可以在https://stackoverflow.com/a/40734360/968577的答案中复制一个列表
如果没有所需的字符串,您的应用程序将以这种方式崩溃。但是,控制台输出将解释问题所在。
发布于 2017-04-23 06:40:18
试着这样做,我希望这会有帮助!首先在info.plist中添加这两点
将这两个委托添加到类文件中。
实现这些委托方法
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{if let PickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage
{
yourimageview.image = PickedImage
dismiss(animated: true, completion: nil)
}
}发布于 2017-10-12 10:39:42
打开相机设置时,我刚刚添加了exit(0)。很好用
https://stackoverflow.com/questions/43567726
复制相似问题