UIImagePickerController didFinishPickingImage没有在Swift 3中调用

时间:2016-10-18 15:53:18

标签: swift uiimagepickercontroller

我正在尝试使用imagePickerView从照片库加载图像。我更新了我的plist以访问Xcode 8下面的照片库。

enter image description here

之后,更新了plist.I可以访问照片库。但是,Picker图像没有加载到我的imageView。

我的代码:

注意: 下面的代码用于Xcode 7而不是Xcode 8?

   import UIKit

   class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {

   let imagePicker = UIImagePickerController()
   @IBOutlet weak var imageView: UIImageView!

   override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
        imagePicker.delegate = self
   }

   @IBAction func library(_ sender: UIButton) {

    if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
        imagePicker.allowsEditing = true
        self.present(imagePicker, animated: true, completion: nil)
     }
   }
来自rmaddy的

代码已更新

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

enter image description here

提前致谢....

1 个答案:

答案 0 :(得分:17)

在Swift 3中,您需要使用正确的委托方法:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.image = image
    }

    picker.dismiss(animated: true, completion: nil);
}