From 0b0c0d2840690b2e2b9baaef5a08a960dc855442 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sun, 5 Jan 2020 20:37:20 +0800 Subject: [PATCH] Add the support for animated coder for thumbnail images --- SDWebImage/Core/SDImageIOAnimatedCoder.m | 37 ++++++++++++++---------- 1 file changed, 21 insertions(+), 16 deletions(-) diff --git a/SDWebImage/Core/SDImageIOAnimatedCoder.m b/SDWebImage/Core/SDImageIOAnimatedCoder.m index 10856ddc..7451a712 100644 --- a/SDWebImage/Core/SDImageIOAnimatedCoder.m +++ b/SDWebImage/Core/SDImageIOAnimatedCoder.m @@ -431,6 +431,22 @@ scale = MAX([scaleFactor doubleValue], 1); } _scale = scale; + CGSize thumbnailSize = CGSizeZero; + NSValue *thumbnailSizeValue = options[SDImageCoderDecodeThumbnailPixelSize]; + if (thumbnailSizeValue != nil) { + #if SD_MAC + thumbnailSize = thumbnailSizeValue.sizeValue; + #else + thumbnailSize = thumbnailSizeValue.CGSizeValue; + #endif + } + _thumbnailSize = thumbnailSize; + BOOL preserveAspectRatio = NO; + NSNumber *preserveAspectRatioValue = options[SDImageCoderDecodePreserveAspectRatio]; + if (preserveAspectRatioValue != nil) { + preserveAspectRatio = preserveAspectRatioValue.boolValue; + } + _preserveAspectRatio = preserveAspectRatio; _imageSource = imageSource; _imageData = data; #if SD_UIKIT @@ -482,24 +498,13 @@ } - (UIImage *)animatedImageFrameAtIndex:(NSUInteger)index { - CGImageRef imageRef = CGImageSourceCreateImageAtIndex(_imageSource, index, NULL); - if (!imageRef) { - return nil; - } + UIImage *image = [self.class createFrameAtIndex:index source:_imageSource scale:_scale preserveAspectRatio:_preserveAspectRatio thumbnailSize:_thumbnailSize]; // Image/IO create CGImage does not decode, so we do this because this is called background queue, this can avoid main queue block when rendering(especially when one more imageViews use the same image instance) - CGImageRef newImageRef = [SDImageCoderHelper CGImageCreateDecoded:imageRef]; - if (!newImageRef) { - newImageRef = imageRef; - } else { - CGImageRelease(imageRef); + UIImage *decodedImage = [SDImageCoderHelper decodedImageWithImage:image]; + if (!decodedImage) { + return image; } -#if SD_MAC - UIImage *image = [[UIImage alloc] initWithCGImage:newImageRef scale:_scale orientation:kCGImagePropertyOrientationUp]; -#else - UIImage *image = [[UIImage alloc] initWithCGImage:newImageRef scale:_scale orientation:UIImageOrientationUp]; -#endif - CGImageRelease(newImageRef); - return image; + return decodedImage; } @end