views:

121

answers:

2

Is there such a thing as an "on load complete" for images in iphone?

I want to be able to destroy a UIActivity indicator once and image is loaded.

What the general best practice for doing this?

A: 

UIImage has a synchronous API. This means that when you create (or draw) a UIImage, it has been loaded and decompressed. This also means that a method call to creating (or drawing) a UIImage can block for as long as it takes to load that image.

If you want to do other things while loading an image, you have to load the image on a background thread, which is possible using the Core Graphics C API. UIImage is not thread safe and can be used on the main thread only.

Nikolai Ruhe
dubbeat
A: 

If you simply want to dismiss a UIActivityIndicatorView after a UIImage has been loaded and displayed properly in a UIImageView, you can try the following:

- (void)loadImageAtPath:(NSString*)path
{
    // show the indicator
    [_myIndicatorView startAnimating];

    // give UIKit a chance to setup and draw the view hierarchy:
    [self performSelector:@selector(_loadImageAtPath:)
               withObject:path
               afterDelay:0];
}

- (void)_loadImageAtPath:(NSString*)path
{
    // load the image
    _myImageView.image = [UIImage imageWithContentsOfFile:path];

    // force the image to load and decompress now
    _myImageView.image.CGImage

    // image is ready, so we can remove the indicator view
    [_myIndicatorView removeFromSuperview];
}

I have to admit, I did not test the above code. Please leave a comment if it works.

Nikolai Ruhe
Thanks again. I'll try implementing this shortly and will let you know the results.
dubbeat