1

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?

2
  • There are a few options. You could detach threads to do the downloads - which would probably get you there the fastest. Commented Mar 3, 2011 at 4:06
  • Thanks for your comment but that isn't what I asked. I know I can detach threads but I was wondering if anyone out there knows the optimal number of concurrent URLConnection's. Commented Mar 3, 2011 at 5:57

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.