Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SDWebImage UITableView Cell Images Not Persisting

I am using SDWebImage library in order to download and cache images and from a web service asynchronously. Following is the method i use to download the images:

- (void) downloadThumbnails:(NSURL *)finalUrl
{
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:finalUrl
                     options:0
                    progress:nil
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
     {
         if (image)
         {
             [self setThumbnail:image];
         }
     }];

}

It is successfully downloading the images from the web service and showing them in the tableview. However its not persisting the images between application runs. Is there anything else i need to do in order to cache the images??

UPDATE: KINDLY DO NOT suggest to use the following:

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

Because, the structure of my program is different. Secondly, if you read description of the SDWebImage library HERE, you will get to know that it can be used the way i am using it using SDWebImageManager.

like image 294
AJ112 Avatar asked Oct 22 '22 07:10

AJ112


2 Answers

I suspect that the reason why the cache mechanism in SDWebImage is not working correctly in this specific case is because it is based on the image URL.

So, if you just download an image and assign it to some of your object's property (like you are doing assigning it to thumbnail), if you later access that property if will simply walk around the cache mechanism.

What you could do to keep the structure of your program is:

  1. store the thumbnailURL in your self object;

  2. when you download the image the first time, call SDWebImageManager downloadWithURL: method as you are doing; this will also store the image in the cache; (this you are already doing as per your code above)

  3. make thumbnail a read-only property that accesses the cache retrieving the image associated to thumbnailURL.

This is the least amount of changes you should do.

This could be an implementation of the read-only property described at point 3:

- (UIImage*)thumbnail {

    return [[SDImageCache sharedCache] imageFromDiskCacheForKey:self.thumbnailURL];
}

where self.thumbnailURL is another property in your object where you save the image URL.

Hope this helps.

like image 156
sergio Avatar answered Nov 03 '22 20:11

sergio


I think you are on wrong path.

If you want to use SDWebImage for UITableView Than Do Following.

1.import UIImageView+WebCache.h by following statement where u have UITableView

#import <SDWebImage/UIImageView+WebCache.h>

2.Write following line in cellForRowAtIndexPath method Of UITableView

[cell.Logo setImageWithURL: finalUrl placeholderImage:[UIImage imageNamed:@"imagename.png"] options:SDWebImageRefreshCached];

  • Replace LOGO with name of your image view in UITableViewCell
  • imagename.png is name of image in project which will display till image not load from internet.

Comment here if u face any problem.

All the best

like image 20
CRDave Avatar answered Nov 03 '22 21:11

CRDave