Merge pull request #3617 from dreampiggy/feat/convenient_encode_api_format_same

Update the convenient API for case when user encode the format the same as image format, provide better quick return
This commit is contained in:
DreamPiggy 2023-10-10 14:31:05 +08:00 committed by GitHub
commit 8b8e70f140
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 16 additions and 8 deletions

View File

@ -372,15 +372,23 @@ static CGFloat SDImageScaleFromPath(NSString *string) {
} }
- (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat compressionQuality:(double)compressionQuality firstFrameOnly:(BOOL)firstFrameOnly { - (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat compressionQuality:(double)compressionQuality firstFrameOnly:(BOOL)firstFrameOnly {
if (firstFrameOnly) { // Protect when user input the imageFormat == self.animatedImageFormat && compressionQuality == 1
// First frame, use super implementation // This should be treated as grabbing `self.animatedImageData` as well :)
return [super sd_imageDataAsFormat:imageFormat compressionQuality:compressionQuality firstFrameOnly:firstFrameOnly]; 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; NSUInteger frameCount = self.animatedImageFrameCount;
if (frameCount <= 1) { if (frameCount <= 1) {
// Static image, use super implementation // Static image
return [super sd_imageDataAsFormat:imageFormat compressionQuality:compressionQuality firstFrameOnly:firstFrameOnly]; imageData = [SDImageCodersManager.sharedManager encodedDataWithImage:self format:imageFormat options:options];
} }
if (imageData) return imageData;
NSUInteger loopCount = self.animatedImageLoopCount;
// Keep animated image encoding, loop each frame. // Keep animated image encoding, loop each frame.
NSMutableArray<SDImageFrame *> *frames = [NSMutableArray arrayWithCapacity:frameCount]; NSMutableArray<SDImageFrame *> *frames = [NSMutableArray arrayWithCapacity:frameCount];
for (size_t i = 0; i < frameCount; i++) { for (size_t i = 0; i < frameCount; i++) {
@ -389,8 +397,7 @@ static CGFloat SDImageScaleFromPath(NSString *string) {
SDImageFrame *frame = [SDImageFrame frameWithImage:image duration:duration]; SDImageFrame *frame = [SDImageFrame frameWithImage:image duration:duration];
[frames addObject:frame]; [frames addObject:frame];
} }
UIImage *animatedImage = [SDImageCoderHelper animatedImageWithFrames:frames]; imageData = [SDImageCodersManager.sharedManager encodedDataWithFrames:frames loopCount:loopCount format:imageFormat options:options];
NSData *imageData = [animatedImage sd_imageDataAsFormat:imageFormat compressionQuality:compressionQuality firstFrameOnly:firstFrameOnly];
return imageData; return imageData;
} }

View File

@ -9,6 +9,7 @@
#import "UIImage+MultiFormat.h" #import "UIImage+MultiFormat.h"
#import "SDImageCodersManager.h" #import "SDImageCodersManager.h"
#import "SDAnimatedImageRep.h" #import "SDAnimatedImageRep.h"
#import "UIImage+Metadata.h"
@implementation UIImage (MultiFormat) @implementation UIImage (MultiFormat)
@ -41,7 +42,7 @@
} }
} }
#endif #endif
return [self sd_imageDataAsFormat:SDImageFormatUndefined]; return [self sd_imageDataAsFormat:self.sd_imageFormat];
} }
- (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat { - (nullable NSData *)sd_imageDataAsFormat:(SDImageFormat)imageFormat {