微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

UIImagePickerController didFinishPickingImage没有在Swift 3中调用

我正在尝试使用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

提前致谢….

解决方法

在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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐