Merge pull request #2378 from dreampiggy/enhancement_FLAnimatedImage_cache

Add cache control for FLAnimatedImage, this allow user to disable memory cache for associated FLAnimatedImage instance
This commit is contained in:
DreamPiggy 2018-07-12 19:21:19 +08:00 committed by GitHub
commit 53ef5e5394
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 25 additions and 3 deletions

View File

@ -52,6 +52,13 @@
*/ */
@property (nonatomic, assign) BOOL sd_predrawingEnabled; @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, create FLAnimatedImage and decoding even when the 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 * 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. * The download is asynchronous and cached.

View File

@ -56,6 +56,19 @@
objc_setAssociatedObject(self, @selector(sd_predrawingEnabled), @(sd_predrawingEnabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC); 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 { - (void)sd_setImageWithURL:(nullable NSURL *)url {
[self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil]; [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil];
} }
@ -113,12 +126,14 @@
FLAnimatedImage *animatedImage; FLAnimatedImage *animatedImage;
// Compatibility in 4.x for lower version FLAnimatedImage. // Compatibility in 4.x for lower version FLAnimatedImage.
if ([FLAnimatedImage respondsToSelector:@selector(initWithAnimatedGIFData:optimalFrameCacheSize:predrawingEnabled:)]) { 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 { } else {
animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData]; animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData];
} }
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
image.sd_FLAnimatedImage = animatedImage; if (weakSelf.sd_cacheFLAnimatedImage) {
image.sd_FLAnimatedImage = animatedImage;
}
weakSelf.animatedImage = animatedImage; weakSelf.animatedImage = animatedImage;
weakSelf.image = nil; weakSelf.image = nil;
if (group) { if (group) {

View File

@ -43,7 +43,7 @@ typedef NS_ENUM(NSUInteger, SDImageCacheConfigExpireType) {
/** /**
* The option to control weak memory cache for images. When enable, `SDImageCache`'s memory cache will use a weak maptable to store the image at the same time when it stored to memory, and get removed at the same time. * The option to control weak memory cache for images. When enable, `SDImageCache`'s memory cache will use a weak maptable to store the image at the same time when it stored to memory, and get removed at the same time.
* However when memory warning triggered, since this weak maptable does not hold a strong reference to image instacnce, even when the memory cache itself is purged, some images which are held strongly by UIImageView or other instance can be recoveried again, to avoid re-query from disk cache or network. This may be helpful for case when app enter background and memory purched, cause cell flashing after re-enter foreground. * However when memory warning is triggered, since the weak maptable does not hold a strong reference to image instacnce, even when the memory cache itself is purged, some images which are held strongly by UIImageViews or other live instances can be recovered again, to avoid later re-query from disk cache or network. This may be helpful for the case, for example, when app enter background and memory is purged, cause cell flashing after re-enter foreground.
* Defautls to YES. You can change this option dynamically. * Defautls to YES. You can change this option dynamically.
*/ */
@property (assign, nonatomic) BOOL shouldUseWeakMemoryCache; @property (assign, nonatomic) BOOL shouldUseWeakMemoryCache;