简体   繁体   中英

iPhone SDK: Download and Set UIImageView before first view controller is displayed

I am having trouble getting a UIImage to display instantly after the app has finished loading and displays the first view controller. There is a delay in the displaying of the image due to it being downloaded from the web. Is there any method that gets called before applicationDidFnishLaunching so the image can be downloaded and displayed instantly ?

Here is the code that I am using to download the images:
In delegate, this method is called in appDidFinishLaunching:

-(void)downloadImages {

    NSString *mainImagesJSON = [[NSString alloc]initWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"mainImagesJSON.php"]]encoding:NSUTF8StringEncoding error:nil];
    SBJsonParser *parser = [[SBJsonParser alloc]init];

    NSDictionary  dictionary1 = [[parser objectWithString:mainImagesJSON error:nil]mutableCopy];


    mainImagesArray = [[dictionary1 valueForKey:@"imgSrc"]mutableCopy];

    NSString *imagesTablePath = [mainImagesArray objectAtIndex:0];
    NSURL *imgURL = [NSURL URLWithString:imagesTablePath];

    NSData *imageData = [NSData dataWithContentsOfURL:imgURL];
    UIImage *image1 = [UIImage imageWithData:imageData];

    imageStorageArray = [[NSMutableArray alloc]init];

    [imageStorageArray addObject:image1];



}

Then in first viewController.m :

AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication]delegate];
UIImage *image1 = [[delegate imageStorageArray]objectAtIndex:0];
[mainHomeImageView setImage:image1];

The way I see it, you have two options:

1: Cache the image in the Library/Caches directory on the first run, and distract the user with some sort of pop-up, or help view.

2: You could make a prolonged splash screen view, which simply displays the splash screen of your app until the file finishes downloading, at which point you switch to your main view.

Generally speaking, it is not a good idea to execute time consuming code in the applicationDidFinishLaunching method as if it takes too long to launch, your app will be killed.

You could display a placeholder image (from project resources) while your image downloads from the web, then replace it with the proper image once it's been downloaded.

I also suggest you look at SDWebImage framework, which is a great framework for downloading and caching images.

This library provides a category for UIImageVIew with support for remote images coming from the web.

It provides:

 An UIImageView category adding web image and cache management to the Cocoa Touch framework An asynchronous image downloader An asynchronous memory + disk image caching with automatic cache expiration handling A guarantee that the same URL won't be downloaded several times A guarantee that bogus URLs won't be retried again and again Performances! 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM