From 233caefec829027b6f073fb5255e9fe66b24f4cb Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Sat, 7 Jul 2018 11:48:09 +0800 Subject: [PATCH 1/2] Add cache control for FLAniamtedImage, this allow user to disable memory cache for associated FLAnimatedImage instance --- .../FLAnimatedImageView+WebCache.h | 7 +++++++ .../FLAnimatedImageView+WebCache.m | 19 +++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h index f046f7a6..680eda3a 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h @@ -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. diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m index 644591b6..8af7a0eb 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m @@ -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) { From 98c28a4419aba39d50d9c99088bbb6dd0f85b38b Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Thu, 12 Jul 2018 01:47:13 +0800 Subject: [PATCH 2/2] Update the comments and fix typo --- SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h | 2 +- SDWebImage/SDImageCacheConfig.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h index 680eda3a..1a0d55b2 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h @@ -54,7 +54,7 @@ /** * 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. + * 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; diff --git a/SDWebImage/SDImageCacheConfig.h b/SDWebImage/SDImageCacheConfig.h index b2ef357d..c0de6888 100644 --- a/SDWebImage/SDImageCacheConfig.h +++ b/SDWebImage/SDImageCacheConfig.h @@ -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. - * 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. */ @property (assign, nonatomic) BOOL shouldUseWeakMemoryCache;