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 {
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<SDImageFrame *> *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;
}

View File

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