5

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.

AJ112
  • 5,291
  • 7
  • 45
  • 60
  • Give cellForRow method code – Durgaprasad Jun 13 '13 at 09:31
  • @Durgaprasad there is no SDWebImage related code in cellForRowAtIndexPath. Just doing this cell.thumbContainer.image = [item thumbnail]; – AJ112 Jun 13 '13 at 09:33
  • give that method code – Durgaprasad Jun 13 '13 at 09:35
  • @Durgaprasad have you checked the update with the code you asked for? – AJ112 Jun 13 '13 at 11:14
  • yes. I cant understand where cell is allocated. Also how [self setThumbnail:image] reflects on [item thumbnail]. Have you stored images in array or in cache. – Durgaprasad Jun 13 '13 at 11:26
  • Did you check if the cache got created on disk? It should be named `com.hackemist.SDWebImageCache.default` in the `Library/Caches` directory of your app. If so, can you QuickLook those files to see if they are your images? And finally: is `finalURL` the same between runs? Does the server change (CDN)? – yonosoytu Jun 17 '13 at 18:58
  • @yonosoytu yes this folder is there on the path you mentioned. and no there is no cdn type of setup, same urls between the application runs – AJ112 Jun 17 '13 at 20:30
  • So I suppose there is no files inside that folder, right? – yonosoytu Jun 17 '13 at 21:09
  • @yonosoytu there are many files in it with weird names. Looks like its persisting the images in this folder but somehow not able to retrive them back – AJ112 Jun 18 '13 at 08:20

3 Answers3

2

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.

sergio
  • 68,819
  • 11
  • 102
  • 123
  • First of all thanks for the answer. Point 1 and 2 in your answer needs a little explanation, if you can provide code that would be great because from the theory i guess my program already implements these two points or may be i am wrong. T – AJ112 Jun 15 '13 at 13:49
  • Please, see my edit. If you are already doing 1 and 2, then the only thing you need is doing point 3 -- now it should be more clear I hope. – sergio Jun 15 '13 at 15:22
1

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

CRDave
  • 9,279
  • 5
  • 41
  • 59
  • Kindly check the UPDATE in my question. Thanks! – AJ112 Jun 15 '13 at 11:30
  • OK. How you find that image is not Cacheing? – CRDave Jun 15 '13 at 13:31
  • If i close and run the app again. The images won't show that means they are not persisted to disk – AJ112 Jun 15 '13 at 13:51
  • OK will you tell me what value you are getting for cacheType and finished? – CRDave Jun 15 '13 at 14:12
  • I haven't used cacheType anywhere. Should i use it? and setThumbnail, thumbnail is an image property, its tied up with the tableview – AJ112 Jun 15 '13 at 14:33
  • cacheType is used by SDWebImage. we can use it to get info about cache. thumbnail is an property of UITableView or UITableViewCell? – CRDave Jun 15 '13 at 14:35
  • thumbnail is a property of another class in which i am downloading the images. UITableView is accessing this property to set images in the tableview cell – AJ112 Jun 15 '13 at 17:08
  • 1
    @CRDave your solution works for me..options:SDWebImageRefreshCached.. i am facing one problem that everytime i load app download images from internet . he is not using cached image so i added options:SDWebImageRefreshCached and it works – Swap-IOS-Android Jun 21 '13 at 10:06
0

i prefer the following method for asynchronous image downloading in tableview.
Actually, you don't need to handle SDWebImageManager yourself.

[cell.imageView setImageWithURL:[NSURL URLWithString:@"IMAGE_URL"]];
[cell.imageView setImageWithURL:[NSURL URLWithString:@"IMAGE_URL"] placeholderImage:[UIImage imageNamed:@"LOCAL_IMAGE_NAME"]];

Make sure you imported the UIImageView+WebCache.h in your implementation. The same above asynchronous image loading is also available for UIButton+WebCache.h category and MKAnnotationView+WebCache.m category.

The new version of SDWebImage also supports the GIF animation using UIImage+GIF.h category
https://github.com/rs/SDWebImage

iNeal
  • 1,729
  • 15
  • 23
  • Kindly check the UPDATE in my question. Of course, i have imported the header and rest of your answer about gif animation is unrelated to my question. Thanks – AJ112 Jun 15 '13 at 11:29