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

Commit f96c019

Browse files
authored
Merge pull request #7 from SDWebImage/feature_cache_cost_function
Adopt SDWebImage's memory cache cost function, fix the nullable issue
2 parents e351c1d + 13dea7a commit f96c019

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

Diff for: SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.h

+2-2
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.

Diff for: SDWebImageFLPlugin/Classes/FLAnimatedImageBridge/SDFLAnimatedImage.m

+20
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,22 @@ - (NSUInteger)animatedImageLoopCount {
125126
}
126127

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

0 commit comments

Comments
 (0)