Search code examples
iosswiftuiimageview

How i detect changes image in a UIImageView in Swift iOS


I am using UIImageView In my UIViewController. In my case, I want to do if my image view changes then let me know if it was changed or not.


Solution

  • Use KVO.

    class ViewController: UIViewController {
        
        @IBOutlet weak var imageView: UIImageView!
        
        private var imageChangeObservation: NSKeyValueObservation?
        
        override func viewDidLoad() {
            super.viewDidLoad()
            
            imageChangeObservation = imageView.observe(\.image, options: [.new]) { [weak self] (object, change) in
                print("Image changed")
            }
        }
    }