Cri1682
Cri1682

Reputation: 513

NSData dataWithContentsOfURL slow

I have some performance problem with NSData dataWithContentsOfURL...

NSURL *url = [NSURL URLWithString:Imagepath];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *img=[UIImage imageWithData:data];
[ArrayImages addObject:img];

This code is placed in a method that manage a JSON response got from an NSUrl connection(after calling my web service). All the code in this method is already in a background thread, moving this piece of code out the background thread do not solve the issue.. All the retrieved image are placed in a view in main thread. What i can do to make dataWithContentsOfURL faster or there is a alternative to dataWithContentsOfURL?

Thanks In Advance

Upvotes: 2

Views: 2747

Answers (2)

justin
justin

Reputation: 104698

+[NSData dataWithContentsOfURL:] is not "slow". If loading one image takes a long time, the problem lies elsewhere.


Evaluate your problem. For starters:

  • Which resource is the bottleneck? Probably the Network.
  • How do you load images? All at once? That would be bad -- display them as they become ready.
  • What are the sizes of the images? I saw one SO question where the poster wanted to load 50 MB images. That's way too large. As well, if all you need is a thumbnail, then be sure you request the thumbnail from the server and load that and not the full-sized image.
  • Are you loading things you don't even need to display? Wait until you need to display them.
  • How many threads are you using for Network tasks? For CPU? For I/O?
  • Are your source images properly "crushed"?
  • Write your program so it flows with your program's presentation model. Example: I had a bazillion images to display in tables, but I made sure to minimize resource usage and made sure load and request cancellation was well supported for the app. This was all coming through the network, and it was plenty fast (it was network-bound).

and if you are loading many images from device storage, you should consider using -[UIImage initWithContentsOfFile:] instead because your image data would not be cached, but can be purged.

Upvotes: 2

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

U need to use Lazy loading for images display as content will displayed as image gets downloaded.

Use SDWebImage which uses lazy loading of images.

Upvotes: 0

Related Questions