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];
Generally speaking, it is not a good idea to execute time consuming code in the
applicationDidFinishLaunchingmethod 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.