GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix the behavior that query thumbnail from full size data does not sy… · SDWebImage/SDWebImage@64c5ff5

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • SDWebImage/Core
    • SDImageCache.m
    • SDWebImageManager.m
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎SDWebImage/Core/SDImageCache.m‎

Copy file name to clipboardExpand all lines: SDWebImage/Core/SDImageCache.m
+43-32Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "UIImage+Metadata.h"
1616
#import "UIImage+ExtendedCacheData.h"
1717
#import "SDCallbackQueue.h"
18+
#import "SDImageTransformer.h" // TODO, remove this
1819

1920
@interface SDImageCacheToken ()
2021

@@ -430,22 +431,9 @@ - (nullable UIImage *)imageFromDiskCacheForKey:(nullable NSString *)key options:
430431
SDImageCacheType cacheType = [context[SDWebImageContextStoreCacheType] integerValue];
431432
shouldCacheToMomery = (cacheType == SDImageCacheTypeAll || cacheType == SDImageCacheTypeMemory);
432433
}
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];
449437
}
450438

451439
return diskImage;
@@ -526,6 +514,42 @@ - (nullable UIImage *)diskImageForKey:(nullable NSString *)key data:(nullable NS
526514
return image;
527515
}
528516

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+
529553
- (void)_unarchiveObjectWithImage:(UIImage *)image forKey:(NSString *)key {
530554
if (!image || !key) {
531555
return;
@@ -655,29 +679,16 @@ - (nullable SDImageCacheToken *)queryCacheOperationForKey:(nullable NSString *)k
655679
SDImageCacheType cacheType = [context[SDWebImageContextStoreCacheType] integerValue];
656680
shouldCacheToMomery = (cacheType == SDImageCacheTypeAll || cacheType == SDImageCacheTypeMemory);
657681
}
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-
}
671682
// 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.
672683
if (shouldCacheToMomery && self.config.shouldCacheImagesInMemory) {
673684
diskImage = [self.memoryCache objectForKey:key];
674685
}
675686
// decode image data only if in-memory cache missed
676687
if (!diskImage) {
677688
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];
681692
}
682693
}
683694
}
Collapse file

‎SDWebImage/Core/SDWebImageManager.m‎

Copy file name to clipboardExpand all lines: SDWebImage/Core/SDWebImageManager.m
+11-1Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,18 @@ - (void)callCacheProcessForOperation:(nonnull SDWebImageCombinedOperation *)oper
302302
if (shouldQueryCache) {
303303
// transformed cache key
304304
NSString *key = [self cacheKeyForURL:url context:context];
305+
// to avoid the SDImageCache's sync logic use the mismatched cache key
306+
// we should strip the `thumbnail` related context
307+
SDWebImageMutableContext *mutableContext;
308+
if (context) {
309+
mutableContext = [context mutableCopy];
310+
} else {
311+
mutableContext = [NSMutableDictionary dictionary];
312+
}
313+
mutableContext[SDWebImageContextImageThumbnailPixelSize] = nil;
314+
mutableContext[SDWebImageContextImagePreserveAspectRatio] = nil;
305315
@weakify(operation);
306-
operation.cacheOperation = [imageCache queryImageForKey:key options:options context:context cacheType:queryCacheType completion:^(UIImage * _Nullable cachedImage, NSData * _Nullable cachedData, SDImageCacheType cacheType) {
316+
operation.cacheOperation = [imageCache queryImageForKey:key options:options context:mutableContext cacheType:queryCacheType completion:^(UIImage * _Nullable cachedImage, NSData * _Nullable cachedData, SDImageCacheType cacheType) {
307317
@strongify(operation);
308318
if (!operation || operation.isCancelled) {
309319
// Image combined operation cancelled by user

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.