简体   繁体   English

如何检测 UIImageView 的图像是否发生了变化

[英]How to detect if the image of UIImageView has changed

I have an app where the user can choose an image from the camera roll and than that image is loaded into an UIImageView .我有一个应用程序,用户可以在其中从相机胶卷中选择一个图像,然后将该图像加载到UIImageView However, if the image is big there is a delay till the selected image actually appears in the UIImageView (Setting the image is not performed on the main thread so the UI does not get frozen).但是,如果图像很大,则在所选图像实际出现在UIImageView之前会有延迟(设置图像不在main thread上执行,因此 UI 不会被冻结)。 Until the image is set, I would like to show an activity indicator which notifies the user that the app did not freeze, its just loading the image.在设置图像之前,我想显示一个活动指示器,通知用户应用程序没有冻结,它只是加载图像。 However I don't know how to detect when the image got set in the UIImageView to hide the activity indicator.但是我不知道如何检测图像何时在UIImageView设置以隐藏活动指示器。 I am guessing maybe KVO could help here, but I am not sure how to implement it properly.我猜也许 KVO 可以在这里提供帮助,但我不确定如何正确实施它。

Sorry for the noob question!抱歉菜鸟问题! :) :)

If you're using swift you can also create UIImageView subclass and override image property to observe the values with didSet .如果您使用 swift,您还可以创建UIImageView子类并覆盖image属性以观察didSet的值。 Just remember to set the image of the superclass.只记得设置超类的图像。

class ImageView: UIImageView {
    override var image: UIImage? {
        didSet {
            super.image = image
            println("Image Set")
        }
    }
}

Observe UIImageView image property using KVO.使用 KVO 观察 UIImageView 图像属性。 Add code to stop spinner in place of NSLog(@"imageReady");.添加代码以停止微调器代替 NSLog(@"imageReady");。

@implementation ViewController {
   __weak IBOutlet UIImageView *_imageView;
}

- (void)viewDidLoad {
   [super viewDidLoad];
   // Do any additional setup after loading the view, typically from a nib.
   [_imageView addObserver:self forKeyPath:@"image" options:NSKeyValueObservingOptionNew context:NULL];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
  if ([keyPath isEqualToString:@"image"]) {
     //Image ready
     //Stop loading spinner.
     NSLog(@"imageReady");
  }
}

1- create an NSKeyValueObservation property 1- 创建一个NSKeyValueObservation属性

2- initialize the property in viewDidLoad with yourImageView.observe... 2- 使用yourImageView.observe...初始化 viewDidLoad 中的属性yourImageView.observe...

3- set it so that it listens for \\.image and set the options to [.old, .new] 3- 设置它以便它侦听\\.image并将选项设置为[.old, .new]

4- remove the observer in deinit 4- 移除 deinit 中的观察者

@IBOutlet var yourImageView: UIImageView!

var observer: NSKeyValueObservation? // 1.

override func viewDidLoad() {
    super.viewDidLoad()

    observer = nil // not necessary but still

    // 2.                            // 3.
    observer = yourImageView.observe(\.image, options: [.old, .new], changeHandler: { [weak self](imageView,_) in
            
        guard let img = imageView.image else { return }

        print("an image was set on your imageView, do something with the image", img)
    })
}

deinit {

    observer = nil // 4.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM