Skip to content

Commit bbad1bc

Browse files
committed
New option SDWebImageRefreshCached
Even if the image is cached, fetch the URL again anyway. When set, NSURLCache is enabled in the downloader via the new option SDWebImageDownloaderEnableNSURLCache. NSURLCache will handle the protocol caching while SDWebImage remains useful for offline images. This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics where the request URL https://graph.facebook.com/[userid]/picture returns a redirect to the actual profile image. If a cached image exists, the completion block is called once with the cached image and again with the final image.
1 parent 95337c4 commit bbad1bc

File tree

4 files changed

+21
-8
lines changed

4 files changed

+21
-8
lines changed

SDWebImage/SDWebImageDownloader.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
typedef enum
1414
{
1515
SDWebImageDownloaderLowPriority = 1 << 0,
16-
SDWebImageDownloaderProgressiveDownload = 1 << 1
16+
SDWebImageDownloaderProgressiveDownload = 1 << 1,
17+
SDWebImageDownloaderEnableNSURLCache = 1 << 2
1718
} SDWebImageDownloaderOptions;
1819

1920
typedef enum

SDWebImage/SDWebImageDownloader.m

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ - (NSInteger)maxConcurrentDownloads
120120
[self addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^
121121
{
122122
// In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests
123-
NSMutableURLRequest *request = [NSMutableURLRequest.alloc initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:15];
123+
NSMutableURLRequest *request = [NSMutableURLRequest.alloc initWithURL:url cachePolicy:(options & SDWebImageDownloaderEnableNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:15];
124124
request.HTTPShouldHandleCookies = NO;
125125
request.HTTPShouldUsePipelining = YES;
126126
request.allHTTPHeaderFields = wself.HTTPHeaders;

SDWebImage/SDWebImageManager.h

+8-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ typedef enum
3131
* This flag enables progressive download, the image is displayed progressively during download as a browser would do.
3232
* By default, the image is only displayed once completely downloaded.
3333
*/
34-
SDWebImageProgressiveDownload = 1 << 3
34+
SDWebImageProgressiveDownload = 1 << 3,
35+
/**
36+
* Even if the image is cached, fetch the URL again anyway. When set, NSURLCache is enabled in the downloader.
37+
* NSURLCache will handle the protocol caching logic while SDWebImage remains useful for offline images.
38+
* This option helps deal with images changing behind the same request URL, e.g. Facebook graph api profile pics.
39+
* If a cached image exists, the completion block is called once with the cached image and again with the final image.
40+
*/
41+
SDWebImageRefreshCached = 1 << 4
3542
} SDWebImageOptions;
3643

3744
typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType);

SDWebImage/SDWebImageManager.m

+10-5
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,21 @@ - (NSString *)cacheKeyForURL:(NSURL *)url
9797
if (image)
9898
{
9999
completedBlock(image, nil, cacheType, YES);
100-
@synchronized(self.runningOperations)
101-
{
102-
[self.runningOperations removeObject:operation];
100+
if (!(options & SDWebImageRefreshCached)) {
101+
@synchronized(self.runningOperations)
102+
{
103+
[self.runningOperations removeObject:operation];
104+
}
103105
}
104106
}
105-
else if (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url])
107+
108+
if ((!image || options & SDWebImageRefreshCached) && (![self.delegate respondsToSelector:@selector(imageManager:shouldDownloadImageForURL:)] || [self.delegate imageManager:self shouldDownloadImageForURL:url]))
106109
{
110+
// download if no image or requested to refresh anyway, and download allowed by delegate
107111
SDWebImageDownloaderOptions downloaderOptions = 0;
108112
if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority;
109113
if (options & SDWebImageProgressiveDownload) downloaderOptions |= SDWebImageDownloaderProgressiveDownload;
114+
if (options & SDWebImageRefreshCached) downloaderOptions |= SDWebImageDownloaderEnableNSURLCache;
110115
__block id<SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished)
111116
{
112117
if (weakOperation.cancelled)
@@ -167,7 +172,7 @@ - (NSString *)cacheKeyForURL:(NSURL *)url
167172
}];
168173
operation.cancelBlock = ^{[subOperation cancel];};
169174
}
170-
else
175+
else if (!image)
171176
{
172177
// Image not in cache and download disallowed by delegate
173178
completedBlock(nil, nil, SDImageCacheTypeNone, YES);

0 commit comments

Comments
 (0)