Merge pull request #3535 from dreampiggy/feature/SDAnimatedImage_scale

Update the implementation `SDScaledImageForScaleFactor` to support SDAnimatedImage
This commit is contained in:
DreamPiggy 2023-05-15 17:58:20 +08:00 committed by GitHub
commit e85d1e5c36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 3 deletions

View File

@ -262,7 +262,7 @@ static NSString * _defaultDiskCacheDirectory;
SDImageFormat format = image.sd_imageFormat;
if (format == SDImageFormatUndefined) {
// If image is animated, use GIF (APNG may be better, but has bugs before macOS 10.14)
if (image.sd_isAnimated) {
if (image.sd_imageFrameCount > 1) {
format = SDImageFormatGIF;
} else {
// If we do not have any data to detect image format, check whether it contains alpha channel to use PNG or JPEG format
@ -461,7 +461,7 @@ static NSString * _defaultDiskCacheDirectory;
if (image) {
if (options & SDImageCacheDecodeFirstFrameOnly) {
// Ensure static image
if (image.sd_isAnimated) {
if (image.sd_imageFrameCount > 1) {
#if SD_MAC
image = [[NSImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:kCGImagePropertyOrientationUp];
#else
@ -593,7 +593,7 @@ static NSString * _defaultDiskCacheDirectory;
if (image) {
if (options & SDImageCacheDecodeFirstFrameOnly) {
// Ensure static image
if (image.sd_isAnimated) {
if (image.sd_imageFrameCount > 1) {
#if SD_MAC
image = [[NSImage alloc] initWithCGImage:image.CGImage scale:image.scale orientation:kCGImagePropertyOrientationUp];
#else

View File

@ -9,6 +9,7 @@
#import "SDWebImageDefine.h"
#import "UIImage+Metadata.h"
#import "NSImage+Compatibility.h"
#import "SDAnimatedImage.h"
#import "SDAssociatedObject.h"
#pragma mark - Image scale
@ -81,6 +82,24 @@ inline UIImage * _Nullable SDScaledImageForScaleFactor(CGFloat scale, UIImage *
return image;
}
UIImage *scaledImage;
// Check SDAnimatedImage support for shortcut
if ([image.class conformsToProtocol:@protocol(SDAnimatedImage)]) {
if ([image respondsToSelector:@selector(animatedCoder)]) {
id<SDAnimatedImageCoder> coder = [(id<SDAnimatedImage>)image animatedCoder];
if (coder) {
scaledImage = [[image.class alloc] initWithAnimatedCoder:coder scale:scale];
}
} else {
// Some class impl does not support `animatedCoder`, keep for compatibility
NSData *data = [(id<SDAnimatedImage>)image animatedImageData];
if (data) {
scaledImage = [[image.class alloc] initWithData:data scale:scale];
}
}
if (scaledImage) {
return scaledImage;
}
}
if (image.sd_isAnimated) {
UIImage *animatedImage;
#if SD_UIKIT || SD_WATCH