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

Commit

Permalink
Merge pull request #7 from SDWebImage/feature_cache_cost_function
Browse files Browse the repository at this point in the history
Adopt SDWebImage's memory cache cost function, fix the nullable issue
  • Loading branch information
dreampiggy authored Jan 8, 2019
2 parents e351c1d + 13dea7a commit f96c019
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
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

0 comments on commit f96c019

Please sign in to comment.