开发者

How to get notified about imageWithContentsOfFile: completion?

I load a hug开发者_JAVA百科e-huge image with imageWithContentsOfFile:, so I have to set up an activityIndicator during the process.

Is there any way/any delegate callback I can use to be informed about the end of this loading process?


imageWithContentsOfFile is synchronous.

You could start an activity indicator, load your big image into memory in a background thread and then go back to the main thread and stop the indicator.

- (void)loadBigImage {
    [activityIndicator startAnimating];
    [self performSelectorInBackground:@selector(loadBigImageInBackground) withObject:nil];
}

- (void)loadBigImageInBackground {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    UIImage *img = [UIImage imageWithContentsOfFile:@"..."];
    [self performSelectorOnMainThread:@selector(bigImageLoaded:) withObject:img waitUntilDone:NO];
    [pool release];
}

- (void)bigImageLoaded:(UIImage *)img {
    [activityIndicator stopAnimating];
    // do stuff 
}


Short answer: Nope. sorry!

Long answer :

You could open the file in a background process (an NSOperation?) bit by bit using C style methods i.e. fopen, fread etc) and fire notifications back to the main thread during the load. Then create the image and fire a notification that the image is ready?


If you want to have a delegate & be informed of the progress of the load, you can use an NSURLConnection instead of the synchronous imageWithContentsOfFile.

There's an example of this in the Apple URL Loading System Programming Guide

Your NSURLConnection delegate didReceiveData: method could append the incoming data to an NSData object, then you would use UIImage imageWithData: to create them image once everything's downloaded.

This gives you the most flexibility/control over monitoring the progress of the load; although if all you're trying to do is avoid hanging the UI while the image downloads, simply using imageWithContentsOfFile in a background thread may be easier.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜