Add cache control for FLAniamtedImage, this allow user to disable memory cache for associated FLAnimatedImage instance

This commit is contained in:
DreamPiggy 2018-07-07 11:48:09 +08:00
parent 2f665fd71f
commit 233caefec8
2 changed files with 24 additions and 2 deletions

View File

@ -52,6 +52,13 @@
*/
@property (nonatomic, assign) BOOL sd_predrawingEnabled;
/**
* Cache control for associated FLAnimatedImage object for memory cache. When enabled, we will bind created FLAnimatedImage instance to UIImage, and store it into memory cache to avoid create this instance cause decoding performance. See `UIImage+FLAnimatedImage`.
* When enabled, this may consume more memory, if you are facing memory issue, disable it and let FLAnimatedImage been created just in time and dealloced as it not been used. However, when disabled, this may impact performance since we need query disk cache when current GIF url is cached.
* Defatuls to YES;
*/
@property (nonatomic, assign) BOOL sd_cacheFLAnimatedImage;
/**
* Load the image at the given url (either from cache or download) and load it in this imageView. It works with both static and dynamic images
* The download is asynchronous and cached.

View File

@ -56,6 +56,19 @@
objc_setAssociatedObject(self, @selector(sd_predrawingEnabled), @(sd_predrawingEnabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (BOOL)sd_cacheFLAnimatedImage {
BOOL cacheFLAnimatedImage = YES;
NSNumber *value = objc_getAssociatedObject(self, @selector(sd_cacheFLAnimatedImage));
if ([value isKindOfClass:[NSNumber class]]) {
cacheFLAnimatedImage = value.boolValue;
}
return cacheFLAnimatedImage;
}
- (void)setSd_cacheFLAnimatedImage:(BOOL)sd_cacheFLAnimatedImage {
objc_setAssociatedObject(self, @selector(sd_cacheFLAnimatedImage), @(sd_cacheFLAnimatedImage), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)sd_setImageWithURL:(nullable NSURL *)url {
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
}
@ -113,12 +126,14 @@
FLAnimatedImage *animatedImage;
// Compatibility in 4.x for lower version FLAnimatedImage.
if ([FLAnimatedImage respondsToSelector:@selector(initWithAnimatedGIFData:optimalFrameCacheSize:predrawingEnabled:)]) {
animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData optimalFrameCacheSize:self.sd_optimalFrameCacheSize predrawingEnabled:self.sd_predrawingEnabled];
animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData optimalFrameCacheSize:weakSelf.sd_optimalFrameCacheSize predrawingEnabled:weakSelf.sd_predrawingEnabled];
} else {
animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData];
}
dispatch_async(dispatch_get_main_queue(), ^{
image.sd_FLAnimatedImage = animatedImage;
if (weakSelf.sd_cacheFLAnimatedImage) {
image.sd_FLAnimatedImage = animatedImage;
}
weakSelf.animatedImage = animatedImage;
weakSelf.image = nil;
if (group) {