diff --git a/CHANGELOG.md b/CHANGELOG.md index 04c166c2..7f82b05c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## [5.4.2 - 5.4 Patch, on Jan 7th, 2020](https://github.com/rs/SDWebImage/releases/tag/5.4.2) +See [all tickets marked for the 5.4.2 release](https://github.com/SDWebImage/SDWebImage/milestone/58) + +### Fixes +- SDAnimatedImage now only keep the animated coder when frame count >=1 , else we will behave like UIImage to save RAM usage #2924 + ## [5.4.1 - 5.4 Patch, on Dec 27th, 2019](https://github.com/rs/SDWebImage/releases/tag/5.4.1) See [all tickets marked for the 5.4.1 release](https://github.com/SDWebImage/SDWebImage/milestone/56) diff --git a/SDWebImage.podspec b/SDWebImage.podspec index 1db2f2cf..446b443a 100644 --- a/SDWebImage.podspec +++ b/SDWebImage.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'SDWebImage' - s.version = '5.4.1' + s.version = '5.4.2' s.osx.deployment_target = '10.10' s.ios.deployment_target = '8.0' diff --git a/SDWebImage/Core/SDAnimatedImage.h b/SDWebImage/Core/SDAnimatedImage.h index feb118e6..569eb88f 100644 --- a/SDWebImage/Core/SDAnimatedImage.h +++ b/SDWebImage/Core/SDAnimatedImage.h @@ -72,6 +72,7 @@ // This class override these methods from UIImage(NSImage), and it supports NSSecureCoding. // You should use these methods to create a new animated image. Use other methods just call super instead. +// Pay attention, when the animated image frame count <= 1, all the `SDAnimatedImage` protocol methods will return nil or 0 value, you'd better check the frame count before usage and keep fallback. + (nullable instancetype)imageNamed:(nonnull NSString *)name; // Cache in memory, no Asset Catalog support #if __has_include() + (nullable instancetype)imageNamed:(nonnull NSString *)name inBundle:(nullable NSBundle *)bundle compatibleWithTraitCollection:(nullable UITraitCollection *)traitCollection; // Cache in memory, no Asset Catalog support diff --git a/SDWebImage/Core/SDAnimatedImage.m b/SDWebImage/Core/SDAnimatedImage.m index ce86e331..422d6578 100644 --- a/SDWebImage/Core/SDAnimatedImage.m +++ b/SDWebImage/Core/SDAnimatedImage.m @@ -156,7 +156,10 @@ static CGFloat SDImageScaleFromPath(NSString *string) { self = [super initWithCGImage:image.CGImage scale:MAX(scale, 1) orientation:image.imageOrientation]; #endif if (self) { - _animatedCoder = animatedCoder; + // Only keep the animated coder if frame count > 1, save RAM usage for non-animated image format (APNG/WebP) + if (animatedCoder.animatedImageFrameCount > 1) { + _animatedCoder = animatedCoder; + } NSData *data = [animatedCoder animatedImageData]; SDImageFormat format = [NSData sd_imageFormatForImageData:data]; _animatedImageFormat = format; @@ -166,6 +169,9 @@ static CGFloat SDImageScaleFromPath(NSString *string) { #pragma mark - Preload - (void)preloadAllFrames { + if (!_animatedCoder) { + return; + } if (!self.isAllFramesLoaded) { NSMutableArray *frames = [NSMutableArray arrayWithCapacity:self.animatedImageFrameCount]; for (size_t i = 0; i < self.animatedImageFrameCount; i++) { @@ -180,6 +186,9 @@ static CGFloat SDImageScaleFromPath(NSString *string) { } - (void)unloadAllFrames { + if (!_animatedCoder) { + return; + } if (self.isAllFramesLoaded) { self.loadedAnimatedImageFrames = nil; self.allFramesLoaded = NO; @@ -190,11 +199,12 @@ static CGFloat SDImageScaleFromPath(NSString *string) { - (instancetype)initWithCoder:(NSCoder *)aDecoder { self = [super initWithCoder:aDecoder]; if (self) { + _animatedImageFormat = [aDecoder decodeIntegerForKey:NSStringFromSelector(@selector(animatedImageFormat))]; NSData *animatedImageData = [aDecoder decodeObjectOfClass:[NSData class] forKey:NSStringFromSelector(@selector(animatedImageData))]; - CGFloat scale = self.scale; if (!animatedImageData) { return self; } + CGFloat scale = self.scale; id animatedCoder = nil; for (idcoder in [SDImageCodersManager sharedManager].coders) { if ([coder conformsToProtocol:@protocol(SDAnimatedImageCoder)]) { @@ -207,15 +217,16 @@ static CGFloat SDImageScaleFromPath(NSString *string) { if (!animatedCoder) { return self; } - _animatedCoder = animatedCoder; - SDImageFormat format = [NSData sd_imageFormatForImageData:animatedImageData]; - _animatedImageFormat = format; + if (animatedCoder.animatedImageFrameCount > 1) { + _animatedCoder = animatedCoder; + } } return self; } - (void)encodeWithCoder:(NSCoder *)aCoder { [super encodeWithCoder:aCoder]; + [aCoder encodeInteger:self.animatedImageFormat forKey:NSStringFromSelector(@selector(animatedImageFormat))]; NSData *animatedImageData = self.animatedImageData; if (animatedImageData) { [aCoder encodeObject:animatedImageData forKey:NSStringFromSelector(@selector(animatedImageData))]; diff --git a/WebImage/Info.plist b/WebImage/Info.plist index beb1d1ec..d243db93 100644 --- a/WebImage/Info.plist +++ b/WebImage/Info.plist @@ -15,11 +15,11 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 5.4.1 + 5.4.2 CFBundleSignature ???? CFBundleVersion - 5.4.1 + 5.4.2 NSPrincipalClass