这就是我的代码目前的样子。我期望发生的是 profileImage (UIImageView) 来显示所选图像,因为它当前没有发生。
import UIKit
class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
@IBOutlet var profileImage: UIImageView!
let picker = UIImagePickerController()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.sourceType = .photoLibrary
}
@IBAction func photoUploadPressed(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
selectedImage = image
profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
}
目前图像选择器出现,但一旦选择它不会改变 UIImageView 的图像。
任何帮助都感激不尽。