[英]ios Swift 3 using Property Observer update Image from UIImagePicker
Hi I'm using IOS swift 3 to let user pick images from library or album. 嗨,我正在使用IOS swift 3让用户从库或相册中选择图像。
I have an UIImage variable. 我有一个UIImage变量。
How can we use property Observer to update the UIImage when user finished pick an Image 用户完成选择图像后,如何使用属性Observer更新UIImage
Some thing like 就像是
var image: UIImage = {
didSet....
}
Currently I'm doing this 目前我正在这样做
func show(image: UIImage) {
imageView.image = image
imageView.isHidden = false
imageView.frame = CGRect(x: 10, y: 10, width: 260, height: 260)
addPhotoLabel.isHidden = true
}
func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any]) {
image = info[UIImagePickerControllerEditedImage] as? UIImage
if let theImage = image {
show(image: theImage)
}
dismiss(animated: true, completion: nil)
}
Thinking of using property Observer to improve the approach. 考虑使用属性观察器来改进方法。
Any help is much appreciate. 任何帮助都非常感谢。
Thanks! 谢谢!
If you really want to update the image view any time the image
property is set, then simply put all of the code in your show
method in the didSet
block for the image
property. 如果您真的想在设置image
属性时随时更新图像视图,则只需将所有代码放在show
方法中image
属性的didSet
块中即可。
var image: UIImage = {
didSet {
imageView.image = image
imageView.isHidden = false
imageView.frame = CGRect(x: 10, y: 10, width: 260, height: 260)
addPhotoLabel.isHidden = true
}
}
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let theImage = info[UIImagePickerControllerEditedImage] as? UIImage {
image = theImage
}
picker.dismiss(animated: true, completion: nil)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.