简体   繁体   English

ios Swift 3使用Property Observer更新UIImagePicker中的图像

[英]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.

相关问题 Unabe使用模拟器从iOS中的UIImagePicker获取图像路径和图像? - Unabe to get image path & image from UIImagePicker in ios using simulator? 在Uift中将选取的图像从UIImagePicker设置为UIImageView - Setting picked image from UIImagePicker to UIImageView in Swift 将图像从UIImagepicker插入到UICollectionViewCell Swift中 - Inserting An Image from UIImagepicker into UICollectionViewCell Swift 在iOS8上使用UIImagePicker结果URL和PHAsset从“我的照片流”加载图像 - Loading image from “My Photo Stream” using UIImagePicker results URL and PHAsset on iOS8 如何在IOS 9上使用UIImagePicker结果URL和PHAsset从媒体库加载图像 - How can i load image from Media Library using UIImagePicker results URL and PHAsset on IOS 9 Swift 2:使用UIImagePicker中的UIImage()var更新UIImage字典 - Swift 2: Update UIImage Dictionary with UIImage() var from UIImagePicker 如何使用属性观察器更新其他控制器中具有相同属性的 swift NSObject 模型? - How to update swift NSObject model having same property in other controllers using property observer? 从UIImagePicker到NSMutableArray的图像 - Image from UIImagePicker to NSMutableArray 来自UIImagePicker的图像的NSURL - NSURL of an image from UIImagePicker iOS试图将图像从uiimagepicker添加到uicollectionview单元格 - IOS trying to add an image from uiimagepicker to a uicollectionview cell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM