从照片库中挑选图像

时间:2016-09-18 14:28:14

标签: ios swift uiimagepickercontroller

我正在关注此Apple Tutorial,我收到此错误:

  

创建具有未知类型的图像格式是错误

从照片库中选择图像时

func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    let selectedImage = info[UIImagePickerControllerOriginalImage] as! UIImage
    photoImageView.image = selectedImage
    dismiss(animated: true, completion: nil)
}

2 个答案:

答案 0 :(得分:0)

试试这个...我就是这样做的。

 class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var photoImageView: UIImageView!
    var standardImg: UIImage!
    var imagePicker: UIImagePickerController!

    override func viewDidLoad() {
        super.viewDidLoad()


    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            standardImg = image
            photoImageView.image = standardImg
        }
            imagePicker.dismiss(animated: true, completion: nil)
    }

    @IBAction func addImage(_ sender: UIButton) {

        imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        present(imagePicker, animated: true, completion: nil)
//        sender.setImage(standardImg, for: UIControlState())

    }
}

答案 1 :(得分:0)

解决方案

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject])
{

    if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
      photoImageView.image = selectedImage
    } 
    else{
       print("Picking image is not in correct format")
    }

    self.dismiss(animated: true, completion: nil)
}