From 4acf569171ba762b4d5a9cbf64487fb3493489b5 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 8 Oct 2023 15:25:10 +0800 Subject: [PATCH] Update the convenient API for case when user encode the format the same as image format, provide better quick return --- SDWebImage/Core/SDAnimatedImage.m | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/SDWebImage/Core/SDAnimatedImage.m b/SDWebImage/Core/SDAnimatedImage.m index f1f0ce15..10f5eb9d 100644 --- a/SDWebImage/Core/SDAnimatedImage.m +++ b/SDWebImage/Core/SDAnimatedImage.m @@ -372,15 +372,23 @@ static CGFloat SDImageScaleFromPath(NSString *string) { } - (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat compressionQuality:(double)compressionQuality firstFrameOnly:(BOOL)firstFrameOnly { - if (firstFrameOnly) { - // First frame, use super implementation - return [super sd_imageDataAsFormat:imageFormat compressionQuality:compressionQuality firstFrameOnly:firstFrameOnly]; + // Protect when user input the imageFormat == self.animatedImageFormat && compressionQuality == 1 + // This should be treated as grabbing `self.animatedImageData` as well :) + NSData *imageData; + if (imageFormat == self.animatedImageFormat && compressionQuality == 1) { + imageData = self.animatedImageData; } + if (imageData) return imageData; + + SDImageCoderOptions *options = @{SDImageCoderEncodeCompressionQuality : @(compressionQuality), SDImageCoderEncodeFirstFrameOnly : @(firstFrameOnly)}; NSUInteger frameCount = self.animatedImageFrameCount; if (frameCount <= 1) { - // Static image, use super implementation - return [super sd_imageDataAsFormat:imageFormat compressionQuality:compressionQuality firstFrameOnly:firstFrameOnly]; + // Static image + imageData = [SDImageCodersManager.sharedManager encodedDataWithImage:self format:imageFormat options:options]; } + if (imageData) return imageData; + + NSUInteger loopCount = self.animatedImageLoopCount; // Keep animated image encoding, loop each frame. NSMutableArray *frames = [NSMutableArray arrayWithCapacity:frameCount]; for (size_t i = 0; i < frameCount; i++) { @@ -389,8 +397,7 @@ static CGFloat SDImageScaleFromPath(NSString *string) { SDImageFrame *frame = [SDImageFrame frameWithImage:image duration:duration]; [frames addObject:frame]; } - UIImage *animatedImage = [SDImageCoderHelper animatedImageWithFrames:frames]; - NSData *imageData = [animatedImage sd_imageDataAsFormat:imageFormat compressionQuality:compressionQuality firstFrameOnly:firstFrameOnly]; + imageData = [SDImageCodersManager.sharedManager encodedDataWithFrames:frames loopCount:loopCount format:imageFormat options:options]; return imageData; }