From 0a0f7e10f6aa8c5d134fae5fdcda66e6ca772a79 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 21 Oct 2023 18:14:38 +0800 Subject: [PATCH] Update the behavior that SDAnimatedImage should pass the path and scale to coder as well This metadata information is important for some edge cases decoding --- SDWebImage/Core/SDAnimatedImage.m | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/SDWebImage/Core/SDAnimatedImage.m b/SDWebImage/Core/SDAnimatedImage.m index c27e2286..75ba2236 100644 --- a/SDWebImage/Core/SDAnimatedImage.m +++ b/SDWebImage/Core/SDAnimatedImage.m @@ -114,7 +114,19 @@ static CGFloat SDImageScaleFromPath(NSString *string) { - (instancetype)initWithContentsOfFile:(NSString *)path { NSData *data = [NSData dataWithContentsOfFile:path]; - return [self initWithData:data scale:SDImageScaleFromPath(path)]; + if (!data) { + return nil; + } + CGFloat scale = SDImageScaleFromPath(path); + // path extension may be useful for coder like raw-image + NSString *fileExtensionHint = path.pathExtension; // without dot + if (fileExtensionHint.length == 0) { + // Ignore file extension which is empty + fileExtensionHint = nil; + } + SDImageCoderMutableOptions *mutableCoderOptions = [NSMutableDictionary dictionaryWithCapacity:1]; + mutableCoderOptions[SDImageCoderDecodeFileExtensionHint] = fileExtensionHint; + return [self initWithData:data scale:scale options:[mutableCoderOptions copy]]; } - (instancetype)initWithData:(NSData *)data { @@ -130,12 +142,17 @@ static CGFloat SDImageScaleFromPath(NSString *string) { return nil; } id animatedCoder = nil; + SDImageCoderMutableOptions *mutableCoderOptions; + if (options != nil) { + mutableCoderOptions = [NSMutableDictionary dictionaryWithDictionary:options]; + } else { + mutableCoderOptions = [NSMutableDictionary dictionaryWithCapacity:1]; + } + mutableCoderOptions[SDImageCoderDecodeScaleFactor] = @(scale); + options = [mutableCoderOptions copy]; for (idcoder in [SDImageCodersManager sharedManager].coders.reverseObjectEnumerator) { if ([coder conformsToProtocol:@protocol(SDAnimatedImageCoder)]) { if ([coder canDecodeFromData:data]) { - if (!options) { - options = @{SDImageCoderDecodeScaleFactor : @(scale)}; - } animatedCoder = [[[coder class] alloc] initWithAnimatedImageData:data options:options]; break; }