【问题标题】:Present ImagePickerView causing crash呈现 ImagePickerView 导致崩溃
【发布时间】:2016-10-08 03:00:11
【问题描述】:

这是我的代码,一个简单的类,在故事板中创建了一个视图,其中包含一个用于呈现 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);
}




}

不知道哪里出了问题,任何帮助都会很棒,谢谢!

【问题讨论】:

  • 您是否要求获得照片的许可?
  • @MikeG 试试我的答案,让我知道它是否有效?
  • 检查我的答案。

标签: ios swift nsexception photolibrary


【解决方案1】:

要访问照片库,您需要将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)

希望对你有帮助。

编码愉快...

【讨论】:

  • 问题是两次调用present(view),我完全忽略了这一点,感谢您指出。
  • 我接受了答案,点赞的目的是什么?
  • @MIke,赞成票是为了让其他人知道这是合适的答案,同时接受更多赞成票,让其他用户有信心使用该答案。
  • 有道理,我同意
【解决方案2】:

当你调用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);
}

【讨论】:

    【解决方案3】:

    试试我的代码,你的问题会很容易解决。

    @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)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-06-29
      • 1970-01-01
      • 2018-01-23
      • 2020-12-02
      • 2013-11-11
      • 2011-01-24
      • 2015-02-23
      • 2011-04-23
      相关资源
      最近更新 更多