From 0dfeebefeb2c5e568d2d75b184f25cfa8b31da7d Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 14 Apr 2018 21:51:35 +0800 Subject: [PATCH] Add `SDImageCacheScaleDownLargeImages` to allow cache to scale down large images if need --- SDWebImage/SDImageCache.h | 7 ++++++- SDWebImage/SDImageCache.m | 9 +++++++-- SDWebImage/SDWebImageManager.m | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/SDWebImage/SDImageCache.h b/SDWebImage/SDImageCache.h index 216151d7..2dd844f4 100644 --- a/SDWebImage/SDImageCache.h +++ b/SDWebImage/SDImageCache.h @@ -33,7 +33,12 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) { /** * By default, we query the memory cache synchronously, disk cache asynchronously. This mask can force to query disk cache synchronously. */ - SDImageCacheQueryDiskSync = 1 << 1 + SDImageCacheQueryDiskSync = 1 << 1, + /** + * By default, images are decoded respecting their original size. On iOS, this flag will scale down the + * images to a size compatible with the constrained memory of devices. + */ + SDImageCacheScaleDownLargeImages = 1 << 2 }; typedef void(^SDCacheQueryCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType); diff --git a/SDWebImage/SDImageCache.m b/SDWebImage/SDImageCache.m index e73b370a..5f87891e 100644 --- a/SDWebImage/SDImageCache.m +++ b/SDWebImage/SDImageCache.m @@ -440,11 +440,16 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { } - (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data { + return [self diskImageForKey:key data:data options:0]; +} + +- (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data options:(SDImageCacheOptions)options { if (data) { UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:data]; image = [self scaledImageForKey:key image:image]; if (self.config.shouldDecompressImages) { - image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&data options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}]; + BOOL shouldScaleDown = options & SDImageCacheScaleDownLargeImages; + image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&data options:@{SDWebImageCoderScaleDownLargeImagesKey: @(shouldScaleDown)}]; } return image; } else { @@ -495,7 +500,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { cacheType = SDImageCacheTypeMemory; } else if (diskData) { // decode image data only if in-memory cache missed - diskImage = [self diskImageForKey:key data:diskData]; + diskImage = [self diskImageForKey:key data:diskData options:options]; if (diskImage && self.config.shouldCacheImagesInMemory) { NSUInteger cost = SDCacheCostForImage(diskImage); [self.memCache setObject:diskImage forKey:key cost:cost]; diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 93a2c005..acfe6c77 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -148,6 +148,7 @@ SDImageCacheOptions cacheOptions = 0; if (options & SDWebImageQueryDataWhenInMemory) cacheOptions |= SDImageCacheQueryDataWhenInMemory; if (options & SDWebImageQueryDiskSync) cacheOptions |= SDImageCacheQueryDiskSync; + if (options & SDWebImageScaleDownLargeImages) cacheOptions |= SDImageCacheScaleDownLargeImages; __weak SDWebImageCombinedOperation *weakOperation = operation; operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key options:cacheOptions done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) {