views:

204

answers:

1

I have a tableView and when the user is selecting one of the cells, im loading a big image.

This loading takes like 10 seconds and i'd like to show a small view with a spinning icon.

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    loadingView = [[LoadingHUDView alloc] initWithTitle: NSLocalizedString(@"Loading image",@"")];
    [self.view addSubview:loadingView];
    [loadingView startAnimating];
    loadingView.center = CGPointMake(self.view.bounds.size.width/2, 150);
    [imageView loadImage: path];
    [loadingView removeFromSuperview];
}

The problem is the view (loadingView) is never shown. It seems like the call to loadImage prevents it to be displayed. Can i force that view to be displayed??

+2  A: 

Hi, the problem is that the loading of the image ties up the thread, so the view is not updated with the spinning icon.

You need to use a different thread, although it then still gets complicated as you cannot easily update the view from a background thread!

So what you actually need to do is kick off the loading of the big image in a background thread.

Put the code to load the big image into another method, and then run it on a background thread as follows:

[self performSelectorInBackground:(@selector(loadBigImage)) withObject:nil];

Remember that inside your -loadBigImage method you'll need to declare an NSAutorelease pool:

-(void)loadBigImage {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    //Code to load big image up
    [pool drain];
}

When this is running in the background your animated loading icon will show up just fine.

Hope that helps

h4xxr
Worked like a charm!! Thank you!!
Jorge
No problem, and welcome to StackOverflow!
h4xxr