在 swift 的图库中显示图像视图中的图像

Show image in imageview from gallery in swift

我是 IOS 的新手,我想在从 gallery.And 中挑选的 imageView 中显示图像 我还在 info.plist.But 中添加了照片描述 图像未显示在 imageView.Here 是我的代码:

import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate,UINavigationControllerDelegate {
    @IBOutlet weak var img: UIImageView!
    @IBOutlet weak var imageView: UIImageView!
    @IBOutlet var chooseBuuton: UIButton!

    override func viewDidLoad() {
        super.viewDidLoad()

    }

    @IBAction func btnClicked() {
        let imagePicker = UIImagePickerController()
        imagePicker.delegate = self
        imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
        imagePicker.allowsEditing = false
        self.present(imagePicker, animated: true, completion: nil)
    }
    private func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
            //imageView.contentMode = .scaleAspectFit
            img.image = pickedImage
        }
        self.dismiss(animated: true, completion: nil)
    }
}

假设您正在使用 Swift 3.x:

您应该按如下方式实施 imagePickerController(_:didFinishPickingMediaWithInfo:)

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

而且应该可以走了。有什么区别?嗯:

1- 移除 private.

2-方法的签名不一样,文档里说了,info应该是[String : Any]类型不是 [String : AnyObject].