22

I am using SDWebImage library to download images from server. https://github.com/rs/SDWebImage

SDWebImage not able update the cached image when image updated on server with the same url.

Shubham
  • 1,230
  • 2
  • 12
  • 26

6 Answers6

20

SDWebImage does some caching by default, so it would be better to use a new URL if the image changes. So, for instance, if you have control over the URL and can change it every time the image has changed, you could do that.

If that's not the case, try using SDWebImageRefreshCached in the options field in order to respect HTTP cache control headers, like this:

[imageView setImageWithURL:[NSURL URLWithString:@"https://graph.facebook.com/olivier.poitrey/picture"]
          placeholderImage:[UIImage imageNamed:@"avatar-placeholder.png"]
                   options:SDWebImageRefreshCached];

See more here

Edgar
  • 2,500
  • 19
  • 31
  • 6
    I have already try this code but its not updating the image. Problem is, When i update the image on the server and move back to previous screen then image is still same there after reloading table view. – Shubham Feb 18 '15 at 15:04
  • You have to make sure the server is setting the right HTTP cache control headers. I just tested a sample project with a Master->Detail structure and a single cell with an image from a URL shared from dropbox, went to the details screen, changed the image on dropbox while maintaining the same name and share URL, made sure dropbox ended syncing, pressed back on the app and the image updated. Take a look at the sample project [RefreshCachedImageTest](https://github.com/Eddpt/RefreshCachedImageTest) – Edgar Feb 18 '15 at 16:08
  • Yes, its working prefect for dropbox images. I think the issue is from server side. Do you any idea about the server side? – Shubham Feb 19 '15 at 09:10
  • Above method worked but deprecated now current method available is this... [photoView setImageWithURL:[NSURL URLWithString:strPhoto] placeholderImage:[UIImage imageNamed:@"imageName"] options:SDWebImageRefreshCached usingActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; – Aanchal Chaurasia Mar 03 '17 at 09:48
  • 1
    this is still not working when images changes on server url....but url is same as before the library does not detect the change in image – Pulkit Kumar Singh Jan 28 '19 at 06:24
13

Update: I've actually written an entire guide about cache, including cache validation https://kean.github.io/blog/image-caching

SDWebImage uses NSURLCache when you set SDWebImageRefreshCached option. Apple's URL loading system implements HTTP cache, including cached responses validation. HTTP cache is quite complex, however there are many beginners guides on HTTP caching:

Basically, the server needs to include some of HTTP cache control headers in each response. There are many different strategies that can be used to implement revalidation. You may use either Last-Modified or ETag. This way each time the client sends a request it will automatically include in your request either Last-Modified or ETag value from previously cached response. If the image hasn't change the server will respond with status code 302 (not modified) and NSURLConnection/NSURLSession will transparently give you a cached response from NSURLCache. You don't have to download the data again, buy you still need to check with the server each time you make a request.

You can also specify an expiration date using HTTP cache control. If the expiration mechanism is used, NSURLConnection/NSURLSession will not revalidate cached response until it is not expired.

For more info about HTTP cache control see the links above. HTTP cache is a universal cache mechanism that should be used whenever possible.

I would recommend to use Nuke framework for image loading (disclaimer: writted by me). It uses NSURLCache by default while still having a memory cache that holds decompressed images.

kean
  • 1,561
  • 14
  • 22
  • on different note , as I am trying Nuke is there a way to just remove a particular image from cache and reload new ? Also A way to clear entire cache so that all images are reloaded from server ? – vishal dharankar Jul 18 '17 at 11:06
6

Here is a code in swift 3 to refresh cache everytime

imgCardBack.sd_setImage(with: URL(string: objUserData.back_image!), placeholderImage:UIImage(named: "cardBack"), options: .refreshCached)
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
3

Swift 4 Simply use the following function in the SDWebImage Library :

SDImageCache.shared().removeImage(forKey: (ImagePath), withCompletion: nil)

This function will delete the saved Cash in memory and Disk , after that just Upload your new Image at the same and it will work perfectly.

Remon Atef
  • 41
  • 4
1

Go to the line number 176 in the file SDWebImageManager.m and change this line

if (options & SDWebImageRefreshCached) downloaderOptions |= SDWebImageDownloaderUseNSURLCache;

to below code.

if (options & SDWebImageRefreshCached) {
      // force progressive off if image already cached but forced refreshing
      downloaderOptions &= ~SDWebImageDownloaderProgressiveDownload;
      // remove SDWebImageDownloaderUseNSURLCache flag
      downloaderOptions &= ~SDWebImageDownloaderUseNSURLCache;
      //ignore image read from NSURLCache if image is cached but force refreshing
       downloaderOptions |= SDWebImageDownloaderIgnoreCachedResponse;
}

For me it worked like a charm.

Purnendu roy
  • 818
  • 8
  • 15
-3

If URL is not changed there is now way for SDWebImage to know that image has been changed on the server.

salabaha
  • 2,468
  • 1
  • 17
  • 18
  • Thanks for reply. The url is same when image updated on server. Problem is there, When i update the image on the server and move back to previous screen then image is still same there after reloading table view. – Shubham Feb 18 '15 at 14:52
  • Yes, image is the same because it is already cached and url is not changed. So SDWebImage can't know that images was changed on the server side. Try [Edgar](http://stackoverflow.com/a/28586476/4525866) solution. – salabaha Feb 18 '15 at 14:59
  • Yes, thats the problem. But when i reload the tableview it should update the image. – Shubham Feb 18 '15 at 15:07
  • When you reload TableView cached images are displayed, because SDWebImage cached your images internally. I mean each time you reload table view SDWebImage checks if it has cached image for particular URL and if so display it. So as long as URL is not changed image will not changed as well. – salabaha Feb 18 '15 at 15:14
  • If you don't have access to the server and can't change URL when image is changed the only solution I see for now is to clear SDWebImage cache for example before reloading table view. – salabaha Feb 18 '15 at 15:16