当前 ImagePickerView 导致崩溃
Present ImagePickerView causing crash
这是我的代码,一个简单的 class,在情节提要中创建了一个视图,其中包含一个用于呈现 imagePickerView 的按钮。显示 imagePickerView,然后应用程序崩溃 libc++abi.dylib: terminating with uncaught exception of type NSException
import Foundation
import UIKit
class ImageSelectionView: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad(){
super.viewDidLoad()
}
@IBAction func backButtonTapped(_ sender: AnyObject) {
self.navigationController?.dismiss(animated: true, completion: nil)
}
@IBAction func openPhotoLibrary(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismiss(animated: true, completion: nil);
}
}
无法弄清楚哪里出了问题,任何帮助都会很棒,谢谢!
要访问照片库,您需要将 Privacy - Photo Library Usage Description
放入应用程序的 .plist
文件中并附上一些说明,
如图所示
特别是在您的代码中,您编写了代码来两次呈现相同的 imagePicker,如下所示。
present(imagePicker, animated: true, completion: nil)
self.present(imagePicker, animated: true, completion:
所以我建议保留一个,可能是
present(imagePicker, animated: true, completion: nil)
或
self.present(imagePicker, animated: true, completion:nil)
希望对您有所帮助。
编码愉快...
当你调用 imagepicker 委托方法时,你的语法是错误的,然后预设了一个 viewcontroller 来选择图像所以替换
present(imagePicker, animated: true, completion: nil)
self.present(imagePicker, animated: true, completion: nil)
与
self.presentViewController(imagePicker, animated: true, completion: nil)
而且你没有在图像视图中显示图像
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
"YOUR IMAGEVIEW".image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: nil);
}
试试我的代码,你的问题会很容易解决。
@IBAction func openPhotoLibrary(_ sender: AnyObject) {
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in
if( UIImagePickerController.isSourceTypeAvailable(.Camera))
{
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .Camera
self.presentViewController(myPickerController, animated: true, completion: nil)
}else
{
let actionController: UIAlertController = UIAlertController(title: "Camera is not available",message: "", preferredStyle: .Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
actionController.addAction(cancelAction)
self.presentViewController(actionController, animated: true, completion: nil)
}
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
"YOURIMAGEVIEW".image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}
这是我的代码,一个简单的 class,在情节提要中创建了一个视图,其中包含一个用于呈现 imagePickerView 的按钮。显示 imagePickerView,然后应用程序崩溃 libc++abi.dylib: terminating with uncaught exception of type NSException
import Foundation
import UIKit
class ImageSelectionView: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
override func viewDidLoad(){
super.viewDidLoad()
}
@IBAction func backButtonTapped(_ sender: AnyObject) {
self.navigationController?.dismiss(animated: true, completion: nil)
}
@IBAction func openPhotoLibrary(_ sender: AnyObject) {
if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
let imagePicker = UIImagePickerController()
imagePicker.delegate = self
imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
imagePicker.allowsEditing = true
present(imagePicker, animated: true, completion: nil)
self.present(imagePicker, animated: true, completion: nil)
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
self.dismiss(animated: true, completion: nil);
}
}
无法弄清楚哪里出了问题,任何帮助都会很棒,谢谢!
要访问照片库,您需要将 Privacy - Photo Library Usage Description
放入应用程序的 .plist
文件中并附上一些说明,
如图所示
特别是在您的代码中,您编写了代码来两次呈现相同的 imagePicker,如下所示。
present(imagePicker, animated: true, completion: nil)
self.present(imagePicker, animated: true, completion:
所以我建议保留一个,可能是
present(imagePicker, animated: true, completion: nil)
或
self.present(imagePicker, animated: true, completion:nil)
希望对您有所帮助。
编码愉快...
当你调用 imagepicker 委托方法时,你的语法是错误的,然后预设了一个 viewcontroller 来选择图像所以替换
present(imagePicker, animated: true, completion: nil)
self.present(imagePicker, animated: true, completion: nil)
与
self.presentViewController(imagePicker, animated: true, completion: nil)
而且你没有在图像视图中显示图像
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]!) {
"YOUR IMAGEVIEW".image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismiss(animated: true, completion: nil);
}
试试我的代码,你的问题会很容易解决。
@IBAction func openPhotoLibrary(_ sender: AnyObject) {
let takePictureAction: UIAlertAction = UIAlertAction(title: "Take Picture", style: .Default) { action -> Void in
if( UIImagePickerController.isSourceTypeAvailable(.Camera))
{
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .Camera
self.presentViewController(myPickerController, animated: true, completion: nil)
}else
{
let actionController: UIAlertController = UIAlertController(title: "Camera is not available",message: "", preferredStyle: .Alert)
let cancelAction: UIAlertAction = UIAlertAction(title: "OK", style: .Cancel) { action -> Void in
//Just dismiss the action sheet
}
actionController.addAction(cancelAction)
self.presentViewController(actionController, animated: true, completion: nil)
}
}
actionSheetController.addAction(takePictureAction)
//Create and add a second option action
let choosePictureAction: UIAlertAction = UIAlertAction(title: "Choose From Camera Roll", style: .Default) { action -> Void in
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = UIImagePickerControllerSourceType.PhotoLibrary
self.presentViewController(myPickerController, animated: true, completion: nil)
}
actionSheetController.addAction(choosePictureAction)
//Present the AlertController
self.presentViewController(actionSheetController, animated: true, completion: nil)
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
"YOURIMAGEVIEW".image = info[UIImagePickerControllerOriginalImage] as? UIImage
self.dismissViewControllerAnimated(true, completion: nil)
}