From 13dea7a4f07ad763d6e364b35f05a385ae7930a8 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 3 Jan 2019 15:54:05 +0800 Subject: [PATCH] Adopt SDWebImage's memory cache cost function, fix the nullable issue --- .../FLAnimatedImageBridge/SDFLAnimatedImage.h | 4 ++-- .../FLAnimatedImageBridge/SDFLAnimatedImage.m | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h index 76a76d4..ef938c9 100644 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h +++ b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h @@ -30,9 +30,9 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPredra @interface SDFLAnimatedImage : UIImage /** - The `FLAnimatedImage` instance for GIF representation + The `FLAnimatedImage` instance for GIF representation. This property should be nonnull. */ -@property (nonatomic, strong, nullable, readonly) FLAnimatedImage *animatedImage; +@property (nonatomic, strong, nonnull, readonly) FLAnimatedImage *animatedImage; /** Create the wrapper with specify `FLAnimatedImage` instance. The instance should be nonnull. diff --git a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m index 58ac34f..a39b54b 100644 --- a/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m +++ b/SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m @@ -7,6 +7,7 @@ */ #import "SDFLAnimatedImage.h" +#import SDWebImageContextOption _Nonnull const SDWebImageContextOptimalFrameCacheSize = @"optimalFrameCacheSize"; SDWebImageContextOption _Nonnull const SDWebImageContextPredrawingEnabled = @"predrawingEnabled"; @@ -125,3 +126,22 @@ - (NSUInteger)animatedImageLoopCount { } @end + +@implementation SDFLAnimatedImage (MemoryCacheCost) + +- (NSUInteger)sd_memoryCost { + NSNumber *value = objc_getAssociatedObject(self, @selector(sd_memoryCost)); + if (value != nil) { + return value.unsignedIntegerValue; + } + + FLAnimatedImage *animatedImage = self.animatedImage; + CGImageRef imageRef = animatedImage.posterImage.CGImage; // / Guaranteed to be loaded, and guaranteed to be CGImage based + NSUInteger bytesPerFrame = CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef); + NSUInteger frameCacheSizeCurrent = animatedImage.frameCacheSizeCurrent; // [1...frame count], more suitable than raw frame count because FLAnimatedImage internal actually store a buffer size but not full frames (they called `window`) + NSUInteger animatedImageCost = frameCacheSizeCurrent * bytesPerFrame; + + return animatedImageCost; +} + +@end