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 { - (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;
} }