optimising multiple asynchronous image downloads in iOS

久未见 提交于 2019-12-06 14:03:51

问题


I want to download a lot of pictures from the server. How can I do this as fast as possible? Currently I am using:

UIImage* myImage = [UIImage imageWithData: 
[NSData dataWithContentsOfURL: 
[NSURL URLWithString: @"http://example.com/image.jpg"]]];

It is painfully slow. Is there any speed increase in downloading multiple images at the same time (asynchronously), and if so how many is too many?


回答1:


You won't get a definitive answer to the optimal number of connections, because there is none. It just depends on several variables such as bandwidth, image size or your own patience. You need to measure this by yourself to get it right.

Doing asynchronous requests won't increase the downloading speed, but the user experience is way better. Seriously, you should consider doing it for any download that takes more than a second.

I always recommend using ASIHTTPRequest, it makes implementing things such as queues and progress bars easy.

Here's the simplest example from an asynchronous request using the library:

- (IBAction)grabURLInBackground:(id)sender
{
   NSURL *url = [NSURL URLWithString:@"http://allseeing-i.com"];
   ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
   [request setDelegate:self];
   [request startAsynchronous];
}

- (void)requestFinished:(ASIHTTPRequest *)request
{
   // Use when fetching text data
   NSString *responseString = [request responseString];

   // Use when fetching binary data
   NSData *responseData = [request responseData];
}

- (void)requestFailed:(ASIHTTPRequest *)request
{
   NSError *error = [request error];
}

Update: This library will no longer be supported. From 1:

"Please note that I am no longer working on this library - you may want to consider using something else for new projects. :)"

Nowadays I use AFNetworking for most of my projects.



来源:https://stackoverflow.com/questions/5176461/optimising-multiple-asynchronous-image-downloads-in-ios

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