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-08 15:25:10 +08:00
parent 936f1c7067
commit 4acf569171
1 changed files with 14 additions and 7 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;
}