Merge pull request #3238 from dreampiggy/fix_image_from_cache_api_behavior
Fix imageFromCacheForKey with options and context behavior, matching the async version one.
This commit is contained in:
commit
fd326e3d51
|
@ -382,6 +382,28 @@ static NSString * _defaultDiskCacheDirectory;
|
|||
- (nullable UIImage *)imageFromCacheForKey:(nullable NSString *)key options:(SDImageCacheOptions)options context:(nullable SDWebImageContext *)context {
|
||||
// First check the in-memory cache...
|
||||
UIImage *image = [self imageFromMemoryCacheForKey:key];
|
||||
if (image) {
|
||||
if (options & SDImageCacheDecodeFirstFrameOnly) {
|
||||
// Ensure static image
|
||||
Class animatedImageClass = image.class;
|
||||
if (image.sd_isAnimated || ([animatedImageClass isSubclassOfClass:[UIImage class]] && [animatedImageClass conformsToProtocol:@protocol(SDAnimatedImage)])) {
|
||||
#if SD_MAC
|
||||
image = [[NSImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:kCGImagePropertyOrientationUp];
|
||||
#else
|
||||
image = [[UIImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:image.imageOrientation];
|
||||
#endif
|
||||
}
|
||||
} else if (options & SDImageCacheMatchAnimatedImageClass) {
|
||||
// Check image class matching
|
||||
Class animatedImageClass = image.class;
|
||||
Class desiredImageClass = context[SDWebImageContextAnimatedImageClass];
|
||||
if (desiredImageClass && ![animatedImageClass isSubclassOfClass:desiredImageClass]) {
|
||||
image = nil;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Since we don't need to query imageData, return image if exist
|
||||
if (image) {
|
||||
return image;
|
||||
}
|
||||
|
|
|
@ -388,6 +388,10 @@ static NSString *kTestImageKeyPNG = @"TestImageKey.png";
|
|||
}];
|
||||
}];
|
||||
|
||||
// Test sync version API `imageFromCacheForKey` as well
|
||||
expect([SDImageCache.sharedImageCache imageFromCacheForKey:kAnimatedImageKey options:SDImageCacheMatchAnimatedImageClass context:@{SDWebImageContextAnimatedImageClass : SDAnimatedImage.class}]).beNil();
|
||||
expect([SDImageCache.sharedImageCache imageFromCacheForKey:kAnimatedImageKey options:0 context:@{SDWebImageContextAnimatedImageClass : SDAnimatedImage.class}]).notTo.beNil();
|
||||
|
||||
[self waitForExpectationsWithCommonTimeout];
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue