How to cache images and label texts downloaded from the internet for every uitableviewcell?

265 Views Asked by At

I download texts and image for every table cell from internet. I do it in background (GCD), but performance is not good (takes a while to dll images for all rows) because there are lots of rows.

I use nsurlconnection for image dll.

I googled a bit and became confused. What is the easiest way to do this? Use NSCache (for images and text) or do i have to learn core data?

4

There are 4 best solutions below

0
On BEST ANSWER

Checkout https://github.com/rs/SDWebImage/ for a super simple way of doing this...

From the README:

[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
               placeholderImage:[UIImage imageNamed:@"placeholder.png"]];

This is all you need for cached images. You can look at the source for how to extend this to support caching text objects too. Alternatively you could use NSUserDefaults to store Key-Value pairs based on URL and text data.

2
On

I am using blocks and ASIHTTPRequest for that and it works fine.

[ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]];

ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:[NSURL urlWithString:<yoururl>]];
[request setDownloadCache:[ASIDownloadCache sharedCache]];
[request setCachePolicy:ASIOnlyLoadIfNotCachedCachePolicy];
[request setCacheStoragePolicy:ASICacheForSessionDurationCacheStoragePolicy];


[request setCompletionBlock:^{
    //do your things
}];

[request setFailedBlock:^{
     //request failed - inform the user
}];

[request startAsynchronous];
0
On

i use this lib to cashe web images
https://github.com/rs/SDWebImage very simple to use

[imageView setImageWithURL:[NSURL URLWithString:yoururl]];

1
On

You can use AsyncImageView to download & cache images.