diff --git a/Examples/SDWebImage Demo/MasterViewController.m b/Examples/SDWebImage Demo/MasterViewController.m index 3a6c1780..f131f55f 100644 --- a/Examples/SDWebImage Demo/MasterViewController.m +++ b/Examples/SDWebImage Demo/MasterViewController.m @@ -115,7 +115,8 @@ cell.customTextLabel.text = [NSString stringWithFormat:@"Image #%ld", (long)indexPath.row]; [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]] placeholderImage:placeholderImage - options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; + options:indexPath.row == 0 ? SDWebImageRefreshCached : 0 + context:@{SDWebImageContextImageThumbnailPixelSize : @(CGSizeMake(180, 120))}]; return cell; } diff --git a/SDWebImage/Core/SDWebImageManager.m b/SDWebImage/Core/SDWebImageManager.m index 61c0a36c..97ffb488 100644 --- a/SDWebImage/Core/SDWebImageManager.m +++ b/SDWebImage/Core/SDWebImageManager.m @@ -95,19 +95,45 @@ static id _defaultImageLoader; } - (nullable NSString *)cacheKeyForURL:(nullable NSURL *)url { - return [self cacheKeyForURL:url cacheKeyFilter:self.cacheKeyFilter]; + return [self cacheKeyForURL:url context:nil]; } -- (nullable NSString *)cacheKeyForURL:(nullable NSURL *)url cacheKeyFilter:(id)cacheKeyFilter { +- (nullable NSString *)cacheKeyForURL:(nullable NSURL *)url context:(nullable SDWebImageContext *)context { if (!url) { return @""; } - - if (cacheKeyFilter) { - return [cacheKeyFilter cacheKeyForURL:url]; - } else { - return url.absoluteString; + + NSString *key; + // Cache Key Filter + id cacheKeyFilter = self.cacheKeyFilter; + if (context[SDWebImageContextCacheKeyFilter]) { + cacheKeyFilter = context[SDWebImageContextCacheKeyFilter]; } + if (cacheKeyFilter) { + key = [cacheKeyFilter cacheKeyForURL:url]; + } else { + key = url.absoluteString; + } + // Thumbnail Key Appending + NSValue *thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize]; + if (thumbnailSizeValue != nil) { + CGSize thumbnailSize = CGSizeZero; +#if SD_MAC + thumbnailSize = thumbnailSizeValue.sizeValue; +#else + thumbnailSize = thumbnailSizeValue.CGSizeValue; +#endif + + BOOL preserveAspectRatio = YES; + NSNumber *preserveAspectRatioValue = context[SDWebImageContextImagePreserveAspectRatio]; + if (preserveAspectRatioValue != nil) { + preserveAspectRatio = preserveAspectRatioValue.boolValue; + } + NSString *transformerKey = [NSString stringWithFormat:@"Thumbnail({%f,%f},%d)", thumbnailSize.width, thumbnailSize.height, preserveAspectRatio]; + key = SDTransformedKeyForKey(key, transformerKey); + } + + return key; } - (SDWebImageCombinedOperation *)loadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDImageLoaderProgressBlock)progressBlock completed:(SDInternalCompletionBlock)completedBlock { @@ -188,8 +214,7 @@ static id _defaultImageLoader; // Check whether we should query cache BOOL shouldQueryCache = !SD_OPTIONS_CONTAINS(options, SDWebImageFromLoaderOnly); if (shouldQueryCache) { - id cacheKeyFilter = context[SDWebImageContextCacheKeyFilter]; - NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter]; + NSString *key = [self cacheKeyForURL:url context:context]; @weakify(operation); operation.cacheOperation = [self.imageCache queryImageForKey:key options:options context:context completion:^(UIImage * _Nullable cachedImage, NSData * _Nullable cachedData, SDImageCacheType cacheType) { @strongify(operation); @@ -303,8 +328,7 @@ static id _defaultImageLoader; if (context[SDWebImageContextOriginalStoreCacheType]) { originalStoreCacheType = [context[SDWebImageContextOriginalStoreCacheType] integerValue]; } - id cacheKeyFilter = context[SDWebImageContextCacheKeyFilter]; - NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter]; + NSString *key = [self cacheKeyForURL:url context:context]; id transformer = context[SDWebImageContextImageTransformer]; id cacheSerializer = context[SDWebImageContextCacheSerializer]; @@ -353,8 +377,7 @@ static id _defaultImageLoader; if (context[SDWebImageContextStoreCacheType]) { storeCacheType = [context[SDWebImageContextStoreCacheType] integerValue]; } - id cacheKeyFilter = context[SDWebImageContextCacheKeyFilter]; - NSString *key = [self cacheKeyForURL:url cacheKeyFilter:cacheKeyFilter]; + NSString *key = [self cacheKeyForURL:url context:context]; id transformer = context[SDWebImageContextImageTransformer]; id cacheSerializer = context[SDWebImageContextCacheSerializer]; BOOL shouldTransformImage = originalImage && (!originalImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)) && transformer;