Permalink
Browse files

#284: Proposed way of dealing with cache keys and grayscale images

  • Loading branch information...
1 parent 6b1f736 commit a09776bfe0c69d4d6fbfaa98d1ea13cb1edd874f @NachoSoto NachoSoto committed Jan 29, 2013
Showing with 11 additions and 9 deletions.
  1. +2 −2 SDWebImage/SDWebImageManager.h
  2. +9 −7 SDWebImage/SDWebImageManager.m
@@ -75,13 +75,13 @@ typedef void(^SDWebImageCompletedWithFinishedBlock)(UIImage *image, NSError *err
* The following example sets a filter in the application delegate that will remove any query-string from the
* URL before to use it as a cache key:
*
- * [[SDWebImageManager sharedManager] setCacheKeyFilter:^(NSURL *url)
+ * [[SDWebImageManager sharedManager] setCacheKeyFilter:^(NSURL *url, BOOL grayscale)
* {
* url = [[NSURL alloc] initWithScheme:url.scheme host:url.host path:url.path];
* return [url absoluteString];
* }];
*/
-@property (strong) NSString *(^cacheKeyFilter)(NSURL *url);
+@property (strong) NSString *(^cacheKeyFilter)(NSURL *url, BOOL grayscale);
/**
* Returns global SDWebImageManager instance.
@@ -48,15 +48,15 @@ - (id)init
}
-- (NSString *)cacheKeyForURL:(NSURL *)url
+- (NSString *)cacheKeyForURL:(NSURL *)url grayscale:(BOOL)grayscale
{
if (self.cacheKeyFilter)
{
- return self.cacheKeyFilter(url);
+ return self.cacheKeyFilter(url, grayscale);
}
else
{
- return [url absoluteString];
+ return [[url absoluteString] stringByAppendingString:(grayscale) ? @"_grayscale" : @""];
}
}
@@ -82,10 +82,12 @@ - (NSString *)cacheKeyForURL:(NSURL *)url
if (completedBlock) completedBlock(nil, nil, SDImageCacheTypeNone, NO);
return operation;
}
-
+
+ const BOOL isGrayscale = (options & SDWebImageGrayscale);
+
[self.runningOperations addObject:operation];
- NSString *key = [self cacheKeyForURL:url];
-
+ NSString *key = [self cacheKeyForURL:url grayscale:isGrayscale];
+
[self.imageCache queryDiskCacheForKey:key done:^(UIImage *image, SDImageCacheType cacheType)
{
if (operation.isCancelled) return;
@@ -100,7 +102,7 @@ - (NSString *)cacheKeyForURL:(NSURL *)url
SDWebImageDownloaderOptions downloaderOptions = 0;
if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority;
if (options & SDWebImageProgressiveDownload) downloaderOptions |= SDWebImageDownloaderProgressiveDownload;
- if (options & SDWebImageGrayscale) downloaderOptions |= SDWebImageDownloaderGrayscale;
+ if (isGrayscale) downloaderOptions |= SDWebImageDownloaderGrayscale;
__block id<SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished)
{
completedBlock(downloadedImage, error, SDImageCacheTypeNone, finished);

0 comments on commit a09776b

Please sign in to comment.