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

Adopt SDWebImage's memory cache cost function, fix the nullable issue #7

Merged
merged 1 commit into from
Jan 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ FOUNDATION_EXPORT SDWebImageContextOption _Nonnull const SDWebImageContextPredra
@interface SDFLAnimatedImage : UIImage <SDAnimatedImage>

/**
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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#import "SDFLAnimatedImage.h"
#import <objc/runtime.h>

SDWebImageContextOption _Nonnull const SDWebImageContextOptimalFrameCacheSize = @"optimalFrameCacheSize";
SDWebImageContextOption _Nonnull const SDWebImageContextPredrawingEnabled = @"predrawingEnabled";
Expand Down Expand Up @@ -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