ellman121 ellman121 - 6 months ago 23
iOS Question

UIImageViewImage doesn't change when set

I've read through about 15 posts on StackOverflow discussing the topic of UIImageViews and animating them to crossfade. I would like to eventually have a crossfade, but I can't even get the images to change at this point.

My code is

var next = 0
let names = ["image_1", "image_2", "image_3", "image_4"]

// While the image is still in memory, keep cycling every few seconds
while ImageView != nil {
print(next % 4)

// Grab the new image
let nextImage = UIImage(named: names[next % 4])
next++

// Set the new image
ImageView.image = nextImage

// Wait a few seconds
NSThread.sleepForTimeInterval(NSTimeInterval(4))
}


The image names are coming out of my xcassets. When I run it, the image doesn't change, but the loop still prints every few seconds

What am I doing wrong?

Answer

You are running this Loop in the main Thread

So the loop blocks your MainThread and the ImageView cannot be updated.

UI-Stuff is not updated immediately when your code is hit.

You can do something like this:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { 
    while ImageView != nil {
        print(next % 4)

        let nextImage = UIImage(named: names[next % 4])
        next++

        dispatch_async(dispatch_get_main_queue()) { 
            // UI Changes need to be done in the main thread
            ImageView.image = nextImage
        }


        // blocking code needs to be done in another thread
        NSThread.sleepForTimeInterval(NSTimeInterval(4))
    }
}