Skip to content
This repository was archived by the owner on Aug 3, 2022. It is now read-only.

Commit 05bf304

Browse files
committed
Adopt SDWebImage's memory cache cost function, fix the nullable issue
1 parent e351c1d commit 05bf304

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPredra
3030
@interface SDFLAnimatedImage : UIImage <SDAnimatedImage>
3131

3232
/**
33-
The `FLAnimatedImage` instance for GIF representation
33+
The `FLAnimatedImage` instance for GIF representation. This property should be nonnull.
3434
*/
35-
@property (nonatomic, strong, nullable, readonly) FLAnimatedImage *animatedImage;
35+
@property (nonatomic, strong, nonnull, readonly) FLAnimatedImage *animatedImage;
3636

3737
/**
3838
Create the wrapper with specify `FLAnimatedImage` instance. The instance should be nonnull.

SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
*/
88

99
#import "SDFLAnimatedImage.h"
10+
#import <objc/runtime.h>
1011

1112
SDWebImageContextOption _Nonnull const SDWebImageContextOptimalFrameCacheSize = @"optimalFrameCacheSize";
1213
SDWebImageContextOption _Nonnull const SDWebImageContextPredrawingEnabled = @"predrawingEnabled";
@@ -125,3 +126,27 @@ - (NSUInteger)animatedImageLoopCount {
125126
}
126127

127128
@end
129+
130+
131+
@interface SDFLAnimatedImage (MemoryCacheCost)
132+
133+
@end
134+
135+
@implementation SDFLAnimatedImage (MemoryCacheCost)
136+
137+
- (NSUInteger)sd_imageMemoryCost {
138+
NSNumber *value = objc_getAssociatedObject(self, @selector(sd_memoryCost));
139+
if (value != nil) {
140+
return value.unsignedIntegerValue;
141+
}
142+
143+
FLAnimatedImage *animatedImage = self.animatedImage;
144+
CGImageRef imageRef = animatedImage.posterImage.CGImage; // / Guaranteed to be loaded, and guaranteed to be CGImage based
145+
NSUInteger bytesPerFrame = CGImageGetBytesPerRow(imageRef) * CGImageGetHeight(imageRef);
146+
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`)
147+
NSUInteger animatedImageCost = frameCacheSizeCurrent * bytesPerFrame;
148+
149+
return animatedImageCost;
150+
}
151+
152+
@end

0 commit comments

Comments
 (0)