Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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). 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. I am guessing maybe KVO could help here, but I am not sure how to implement it properly.

Sorry for the noob question! :)

like image 316
Balázs Vincze Avatar asked Jul 31 '15 20:07

Balázs Vincze


1 Answers

If you're using swift you can also create UIImageView subclass and override image property to observe the values with didSet. Just remember to set the image of the superclass.

class ImageView: UIImageView {
    override var image: UIImage? {
        didSet {
            super.image = image
            println("Image Set")
        }
    }
}
like image 65
libec Avatar answered Oct 27 '22 21:10

libec