How to load content fast with images loading in background?

大兔子大兔子 提交于 2019-12-13 08:22:49

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!