|
15 | 15 | #import "UIImage+Metadata.h" |
16 | 16 | #import "UIImage+ExtendedCacheData.h" |
17 | 17 | #import "SDCallbackQueue.h" |
| 18 | +#import "SDImageTransformer.h" // TODO, remove this |
18 | 19 |
|
19 | 20 | @interface SDImageCacheToken () |
20 | 21 |
|
@@ -430,22 +431,9 @@ - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key options: |
430 | 431 | SDImageCacheType cacheType = [context[SDWebImageContextStoreCacheType] integerValue]; |
431 | 432 | shouldCacheToMomery = (cacheType == SDImageCacheTypeAll || cacheType == SDImageCacheTypeMemory); |
432 | 433 | } |
433 | | - CGSize thumbnailSize = CGSizeZero; |
434 | | - NSValue *thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize]; |
435 | | - if (thumbnailSizeValue != nil) { |
436 | | -#if SD_MAC |
437 | | - thumbnailSize = thumbnailSizeValue.sizeValue; |
438 | | -#else |
439 | | - thumbnailSize = thumbnailSizeValue.CGSizeValue; |
440 | | -#endif |
441 | | - } |
442 | | - if (thumbnailSize.width > 0 && thumbnailSize.height > 0) { |
443 | | - // Query full size cache key which generate a thumbnail, should not write back to full size memory cache |
444 | | - shouldCacheToMomery = NO; |
445 | | - } |
446 | | - if (shouldCacheToMomery && diskImage && self.config.shouldCacheImagesInMemory) { |
447 | | - NSUInteger cost = diskImage.sd_memoryCost; |
448 | | - [self.memoryCache setObject:diskImage forKey:key cost:cost]; |
| 434 | + if (shouldCacheToMomery) { |
| 435 | + // check if we need sync logic |
| 436 | + [self _syncDiskToMemoryWithImage:diskImage forKey:key]; |
449 | 437 | } |
450 | 438 |
|
451 | 439 | return diskImage; |
@@ -526,6 +514,42 @@ - (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NS |
526 | 514 | return image; |
527 | 515 | } |
528 | 516 |
|
| 517 | +- (void)_syncDiskToMemoryWithImage:(UIImage *)diskImage forKey:(NSString *)key { |
| 518 | + // earily check |
| 519 | + if (!self.config.shouldCacheImagesInMemory) { |
| 520 | + return; |
| 521 | + } |
| 522 | + if (!diskImage) { |
| 523 | + return; |
| 524 | + } |
| 525 | + // The disk -> memory sync logic, which should only store thumbnail image with thumbnail key |
| 526 | + // However, caller (like SDWebImageManager) will query full key, with thumbnail size, and get thubmnail image |
| 527 | + // We should add a check here, currently it's a hack |
| 528 | + if (diskImage.sd_isThumbnail) { |
| 529 | + SDImageCoderOptions *options = diskImage.sd_decodeOptions; |
| 530 | + CGSize thumbnailSize = CGSizeZero; |
| 531 | + NSValue *thumbnailSizeValue = options[SDImageCoderDecodeThumbnailPixelSize]; |
| 532 | + if (thumbnailSizeValue != nil) { |
| 533 | + #if SD_MAC |
| 534 | + thumbnailSize = thumbnailSizeValue.sizeValue; |
| 535 | + #else |
| 536 | + thumbnailSize = thumbnailSizeValue.CGSizeValue; |
| 537 | + #endif |
| 538 | + } |
| 539 | + BOOL preserveAspectRatio = YES; |
| 540 | + NSNumber *preserveAspectRatioValue = options[SDImageCoderDecodePreserveAspectRatio]; |
| 541 | + if (preserveAspectRatioValue != nil) { |
| 542 | + preserveAspectRatio = preserveAspectRatioValue.boolValue; |
| 543 | + } |
| 544 | + // Calculate the actual thumbnail key |
| 545 | + NSString *thumbnailKey = SDThumbnailedKeyForKey(key, thumbnailSize, preserveAspectRatio); |
| 546 | + // Override the sync key |
| 547 | + key = thumbnailKey; |
| 548 | + } |
| 549 | + NSUInteger cost = diskImage.sd_memoryCost; |
| 550 | + [self.memoryCache setObject:diskImage forKey:key cost:cost]; |
| 551 | +} |
| 552 | + |
529 | 553 | - (void)_unarchiveObjectWithImage:(UIImage *)image forKey:(NSString *)key { |
530 | 554 | if (!image || !key) { |
531 | 555 | return; |
@@ -655,29 +679,16 @@ - (nullable SDImageCacheToken *)queryCacheOperationForKey:(nullable NSString *)k |
655 | 679 | SDImageCacheType cacheType = [context[SDWebImageContextStoreCacheType] integerValue]; |
656 | 680 | shouldCacheToMomery = (cacheType == SDImageCacheTypeAll || cacheType == SDImageCacheTypeMemory); |
657 | 681 | } |
658 | | - CGSize thumbnailSize = CGSizeZero; |
659 | | - NSValue *thumbnailSizeValue = context[SDWebImageContextImageThumbnailPixelSize]; |
660 | | - if (thumbnailSizeValue != nil) { |
661 | | - #if SD_MAC |
662 | | - thumbnailSize = thumbnailSizeValue.sizeValue; |
663 | | - #else |
664 | | - thumbnailSize = thumbnailSizeValue.CGSizeValue; |
665 | | - #endif |
666 | | - } |
667 | | - if (thumbnailSize.width > 0 && thumbnailSize.height > 0) { |
668 | | - // Query full size cache key which generate a thumbnail, should not write back to full size memory cache |
669 | | - shouldCacheToMomery = NO; |
670 | | - } |
671 | 682 | // Special case: If user query image in list for the same URL, to avoid decode and write **same** image object into disk cache multiple times, we query and check memory cache here again. |
672 | 683 | if (shouldCacheToMomery && self.config.shouldCacheImagesInMemory) { |
673 | 684 | diskImage = [self.memoryCache objectForKey:key]; |
674 | 685 | } |
675 | 686 | // decode image data only if in-memory cache missed |
676 | 687 | if (!diskImage) { |
677 | 688 | diskImage = [self diskImageForKey:key data:diskData options:options context:context]; |
678 | | - if (shouldCacheToMomery && diskImage && self.config.shouldCacheImagesInMemory) { |
679 | | - NSUInteger cost = diskImage.sd_memoryCost; |
680 | | - [self.memoryCache setObject:diskImage forKey:key cost:cost]; |
| 689 | + // check if we need sync logic |
| 690 | + if (shouldCacheToMomery) { |
| 691 | + [self _syncDiskToMemoryWithImage:diskImage forKey:key]; |
681 | 692 | } |
682 | 693 | } |
683 | 694 | } |
|