问题
I want to make image load in background and let content load first. I tried below code but didnt work. What is wrong? Help me.
Thank you in advance.
- (void)viewDidLoad
{
[super viewDidLoad];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"site.com/json.php"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:self.responseData options:NSJSONReadingMutableLeaves error:&myError];
self.recipes = [[NSMutableArray alloc] init];
for(NSDictionary *dic in res){
Recipe *recipe = [[Recipe alloc] init];
recipe.name = [dic objectForKey:@"title"];
recipe.imageName = [NSString stringWithFormat:@"site.com/iphone_images/%@",[dic objectForKey:@"imageName"]];
[recipes addObject:recipe];
[myTableView reloadData];
}
}
回答1:
First get content (which you need) and url of all the images , show all the content and then start downloading of all image in background.
回答2:
Please use AFNetworking to download the contents,also to download the images in the background ,AFNetworking comes with the UIImageView category which would be very much helpful.
回答3:
Here's code that is part of a complete downloadable example showing how to put up a UITableView, download the related images in the background, and add them to the table when they arrive:
https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch24p842downloader/ch37p1099downloader/MyTableViewController.m
来源:https://stackoverflow.com/questions/22117887/how-to-load-content-fast-with-images-loading-in-background