From e9527b393afb093e9cba094eb5bad2bb4a327f69 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Wed, 16 May 2018 15:35:55 +0800 Subject: [PATCH] Add optimalFrameCacheSize && predrawingEnabled options for FLAnimatedImage. It makes users to set optimal frame cache size of FLAnimatedImage after image load. --- .../FLAnimatedImageView+WebCache.h | 14 ++++++++ .../FLAnimatedImageView+WebCache.m | 35 ++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h index 6d2788b8..f046f7a6 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.h @@ -38,6 +38,20 @@ */ @interface FLAnimatedImageView (WebCache) +/** + * Optimal frame cache size of FLAnimatedImage during initializer. (1.0.11 version later) + * This value will help you set `optimalFrameCacheSize` arg of FLAnimatedImage initializer after image load. + * Defaults to 0. + */ +@property (nonatomic, assign) NSUInteger sd_optimalFrameCacheSize; + +/** + * Predrawing control of FLAnimatedImage during initializer. (1.0.11 version later) + * This value will help you set `predrawingEnabled` arg of FLAnimatedImage initializer after image load. + * Defaults to YES. + */ +@property (nonatomic, assign) BOOL sd_predrawingEnabled; + /** * 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 773d4f0b..644591b6 100644 --- a/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m +++ b/SDWebImage/FLAnimatedImage/FLAnimatedImageView+WebCache.m @@ -29,6 +29,33 @@ @implementation FLAnimatedImageView (WebCache) +// These property based options will moved to `SDWebImageContext` in 5.x, to allow per-image-request level options instead of per-imageView-level options +- (NSUInteger)sd_optimalFrameCacheSize { + NSUInteger optimalFrameCacheSize = 0; + NSNumber *value = objc_getAssociatedObject(self, @selector(sd_optimalFrameCacheSize)); + if ([value isKindOfClass:[NSNumber class]]) { + optimalFrameCacheSize = value.unsignedShortValue; + } + return optimalFrameCacheSize; +} + +- (void)setSd_optimalFrameCacheSize:(NSUInteger)sd_optimalFrameCacheSize { + objc_setAssociatedObject(self, @selector(sd_optimalFrameCacheSize), @(sd_optimalFrameCacheSize), OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + +- (BOOL)sd_predrawingEnabled { + BOOL predrawingEnabled = YES; + NSNumber *value = objc_getAssociatedObject(self, @selector(sd_predrawingEnabled)); + if ([value isKindOfClass:[NSNumber class]]) { + predrawingEnabled = value.boolValue; + } + return predrawingEnabled; +} + +- (void)setSd_predrawingEnabled:(BOOL)sd_predrawingEnabled { + objc_setAssociatedObject(self, @selector(sd_predrawingEnabled), @(sd_predrawingEnabled), OBJC_ASSOCIATION_RETAIN_NONATOMIC); +} + - (void)sd_setImageWithURL:(nullable NSURL *)url { [self sd_setImageWithURL:url placeholderImage:nil options:0 progress:nil completed:nil]; } @@ -83,7 +110,13 @@ weakSelf.animatedImage = nil; // Secondly create FLAnimatedImage in global queue because it's time consuming, then set it back dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ - FLAnimatedImage *animatedImage = [FLAnimatedImage animatedImageWithGIFData:imageData]; + 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]; + } else { + animatedImage = [[FLAnimatedImage alloc] initWithAnimatedGIFData:imageData]; + } dispatch_async(dispatch_get_main_queue(), ^{ image.sd_FLAnimatedImage = animatedImage; weakSelf.animatedImage = animatedImage;