Committed by
GitHub
Merge pull request #2281 from dreampiggy/feature_scale_download_cache_options
Add `SDImageCacheScaleDownLargeImages` to allow cache to scale down large images if need
Showing
3 changed files
with
14 additions
and
3 deletions
@@ -33,7 +33,12 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) { | @@ -33,7 +33,12 @@ typedef NS_OPTIONS(NSUInteger, SDImageCacheOptions) { | ||
33 | /** | 33 | /** |
34 | * By default, we query the memory cache synchronously, disk cache asynchronously. This mask can force to query disk cache synchronously. | 34 | * By default, we query the memory cache synchronously, disk cache asynchronously. This mask can force to query disk cache synchronously. |
35 | */ | 35 | */ |
36 | - SDImageCacheQueryDiskSync = 1 << 1 | 36 | + SDImageCacheQueryDiskSync = 1 << 1, |
37 | + /** | ||
38 | + * By default, images are decoded respecting their original size. On iOS, this flag will scale down the | ||
39 | + * images to a size compatible with the constrained memory of devices. | ||
40 | + */ | ||
41 | + SDImageCacheScaleDownLargeImages = 1 << 2 | ||
37 | }; | 42 | }; |
38 | 43 | ||
39 | typedef void(^SDCacheQueryCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType); | 44 | typedef void(^SDCacheQueryCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType); |
@@ -440,11 +440,16 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | @@ -440,11 +440,16 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | ||
440 | } | 440 | } |
441 | 441 | ||
442 | - (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data { | 442 | - (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data { |
443 | + return [self diskImageForKey:key data:data options:0]; | ||
444 | +} | ||
445 | + | ||
446 | +- (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NSData *)data options:(SDImageCacheOptions)options { | ||
443 | if (data) { | 447 | if (data) { |
444 | UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:data]; | 448 | UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:data]; |
445 | image = [self scaledImageForKey:key image:image]; | 449 | image = [self scaledImageForKey:key image:image]; |
446 | if (self.config.shouldDecompressImages) { | 450 | if (self.config.shouldDecompressImages) { |
447 | - image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&data options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}]; | 451 | + BOOL shouldScaleDown = options & SDImageCacheScaleDownLargeImages; |
452 | + image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&data options:@{SDWebImageCoderScaleDownLargeImagesKey: @(shouldScaleDown)}]; | ||
448 | } | 453 | } |
449 | return image; | 454 | return image; |
450 | } else { | 455 | } else { |
@@ -495,7 +500,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | @@ -495,7 +500,7 @@ FOUNDATION_STATIC_INLINE NSUInteger SDCacheCostForImage(UIImage *image) { | ||
495 | cacheType = SDImageCacheTypeMemory; | 500 | cacheType = SDImageCacheTypeMemory; |
496 | } else if (diskData) { | 501 | } else if (diskData) { |
497 | // decode image data only if in-memory cache missed | 502 | // decode image data only if in-memory cache missed |
498 | - diskImage = [self diskImageForKey:key data:diskData]; | 503 | + diskImage = [self diskImageForKey:key data:diskData options:options]; |
499 | if (diskImage && self.config.shouldCacheImagesInMemory) { | 504 | if (diskImage && self.config.shouldCacheImagesInMemory) { |
500 | NSUInteger cost = SDCacheCostForImage(diskImage); | 505 | NSUInteger cost = SDCacheCostForImage(diskImage); |
501 | [self.memCache setObject:diskImage forKey:key cost:cost]; | 506 | [self.memCache setObject:diskImage forKey:key cost:cost]; |
@@ -148,6 +148,7 @@ | @@ -148,6 +148,7 @@ | ||
148 | SDImageCacheOptions cacheOptions = 0; | 148 | SDImageCacheOptions cacheOptions = 0; |
149 | if (options & SDWebImageQueryDataWhenInMemory) cacheOptions |= SDImageCacheQueryDataWhenInMemory; | 149 | if (options & SDWebImageQueryDataWhenInMemory) cacheOptions |= SDImageCacheQueryDataWhenInMemory; |
150 | if (options & SDWebImageQueryDiskSync) cacheOptions |= SDImageCacheQueryDiskSync; | 150 | if (options & SDWebImageQueryDiskSync) cacheOptions |= SDImageCacheQueryDiskSync; |
151 | + if (options & SDWebImageScaleDownLargeImages) cacheOptions |= SDImageCacheScaleDownLargeImages; | ||
151 | 152 | ||
152 | __weak SDWebImageCombinedOperation *weakOperation = operation; | 153 | __weak SDWebImageCombinedOperation *weakOperation = operation; |
153 | operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key options:cacheOptions done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) { | 154 | operation.cacheOperation = [self.imageCache queryCacheOperationForKey:key options:cacheOptions done:^(UIImage *cachedImage, NSData *cachedData, SDImageCacheType cacheType) { |
-
Please register or login to post a comment