views:

45

answers:

2

I have an relatively image in local storage, I want to show it to the user without disturbing UI thread. I'm currently using

[[UIImage alloc] initWithContentsOfFile:path];

to load image.

Any suggestions/help please....

A: 

You can load the image data asynchronously by following the approach described in the answer to this question: http://stackoverflow.com/questions/3111543

GregInYEG
+3  A: 

If all you're trying to do is keep the UI thread available, set up a short method to load it in the background and update the imageView when done:

-(void)backgroundLoadImageFromPath:(NSString*)path {
    UIImage *newImage = [UIImage imageWithContentsOfFile:path];
    [myImageView performSelectorOnMainThread:@selector(setImage:) withObject:newImage waitUntilDone:YES];
}

This presumes myImageView is a member variable of the class. Now, simply run it in the background from any thread:

[self performSelectorInBackground:@selector(backgroundLoadImageFromPath:) withObject:path];

Note, in backgroundLoadImageFromPath you need to wait until the setImage: selector finishes, otherwise the background thread's autorelease pool may deallocate the image before the setImage: method can retain it.

John Franklin