使用 UIImagePicker 在 UIImageView 中设置图像
Use UIImagePicker to set the image in a UIImageView
这是我的代码目前的样子。我期望发生的是 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 的图像。
任何帮助将不胜感激。
它按预期工作,您的代码中唯一的错误是您在 photoUploadPressed(_ sender: Any)
中缺少 } 并且您没有声明 selectedImage 并在 imagePickerController
中使用它
工作代码
import UIKit
class ViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let picker = UIImagePickerController()
var selectedImage: UIImage?
@IBOutlet weak var profileImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
picker.delegate = self
picker.sourceType = .photoLibrary
}
@IBAction func potoUploadPressed(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}// It is missing in your code
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
selectedImage = image // You have not declared selectedImage
profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
}
这是我的代码目前的样子。我期望发生的是 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 的图像。
任何帮助将不胜感激。
它按预期工作,您的代码中唯一的错误是您在 photoUploadPressed(_ sender: Any)
中缺少 } 并且您没有声明 selectedImage 并在 imagePickerController
工作代码
import UIKit
class ViewController: UIViewController , UIImagePickerControllerDelegate, UINavigationControllerDelegate{
let picker = UIImagePickerController()
var selectedImage: UIImage?
@IBOutlet weak var profileImage: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
picker.delegate = self
picker.sourceType = .photoLibrary
}
@IBAction func potoUploadPressed(_ sender: Any) {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self;
myPickerController.sourceType = .photoLibrary
self.present(myPickerController, animated: true, completion: nil)
}// It is missing in your code
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
if let image = info[UIImagePickerController.InfoKey.originalImage] as? UIImage{
selectedImage = image // You have not declared selectedImage
profileImage.image = image
}
dismiss(animated: true, completion: nil)
}
}