我是IOS的新手,我想在图像中显示图像,这是从gallery.And我也添加了照片描述在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)
}
}
答案 0 :(得分:0)
假设您使用的是 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]
。