From f097ef7558f46617a4fffe3a67134b6da16c3054 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Tue, 12 Mar 2013 14:29:47 +0100 Subject: [PATCH] Fix NSURLResponse not cached when `SDWebImageDownloaderEnableNSURLCache` is passed #326 --- SDWebImage/SDWebImageDownloader.m | 2 +- SDWebImage/SDWebImageDownloaderOperation.m | 11 +++++++++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 1dbe85a..8e776fd 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -119,7 +119,7 @@ static NSString *const kCompletedCallbackKey = @"completed"; [self addProgressCallback:progressBlock andCompletedBlock:completedBlock forURL:url createCallback:^ { - // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests + // In order to prevent from potential duplicate caching (NSURLCache + SDImageCache) we disable the cache for image requests if told otherwise NSMutableURLRequest *request = [NSMutableURLRequest.alloc initWithURL:url cachePolicy:(options & SDWebImageDownloaderEnableNSURLCache ? NSURLRequestUseProtocolCachePolicy : NSURLRequestReloadIgnoringLocalCacheData) timeoutInterval:15]; request.HTTPShouldHandleCookies = NO; request.HTTPShouldUsePipelining = YES; diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 1dbc069..cbe45d8 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -311,8 +311,15 @@ - (NSCachedURLResponse *)connection:(NSURLConnection *)connection willCacheResponse:(NSCachedURLResponse *)cachedResponse { - // Prevents caching of responses - return nil; + if (self.request.cachePolicy == NSURLRequestReloadIgnoringLocalCacheData) + { + // Prevents caching of responses + return nil; + } + else + { + return cachedResponse; + } }