From 1abc05e05c617b398a08ad8653ba0db289a83aed Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Tue, 27 Aug 2019 17:40:05 +0800 Subject: [PATCH] Add new properties `clearBufferWhenStopped` and `resetFrameIndexWhenStopped` --- SDWebImage/Core/SDAnimatedImageView.h | 14 ++++++++++ SDWebImage/Core/SDAnimatedImageView.m | 37 +++++++++++++++++++-------- 2 files changed, 41 insertions(+), 10 deletions(-) diff --git a/SDWebImage/Core/SDAnimatedImageView.h b/SDWebImage/Core/SDAnimatedImageView.h index ec5bda13..abf4e5d7 100644 --- a/SDWebImage/Core/SDAnimatedImageView.h +++ b/SDWebImage/Core/SDAnimatedImageView.h @@ -58,6 +58,20 @@ */ @property (nonatomic, assign) BOOL shouldIncrementalLoad; +/** + Whether or not to clear the frame buffer cache when animation stopped. See `maxBufferSize` + This is useful when you want to limit the memory usage during frequently visibility changes (such as image view inside a list view, then push and pop) + Default is NO. + */ +@property (nonatomic, assign) BOOL clearBufferWhenStopped; + +/** + Whether or not to reset the current frame index when animation stopped. + For some of use case, you may want to reset the frame index to 0 when stop, but some other want to keep the current frame index. + Default is NO. + */ +@property (nonatomic, assign) BOOL resetFrameIndexWhenStopped; + #if SD_UIKIT /** You can specify a runloop mode to let it rendering. diff --git a/SDWebImage/Core/SDAnimatedImageView.m b/SDWebImage/Core/SDAnimatedImageView.m index 9c4dddd0..672d9086 100644 --- a/SDWebImage/Core/SDAnimatedImageView.m +++ b/SDWebImage/Core/SDAnimatedImageView.m @@ -149,21 +149,15 @@ static NSUInteger SDDeviceFreeMemory() { self.animatedImage = nil; self.totalFrameCount = 0; self.totalLoopCount = 0; - self.currentFrame = nil; - self.currentFrameIndex = 0; - self.currentLoopCount = 0; - self.currentTime = 0; - self.bufferMiss = NO; + // reset current state + [self resetCurrentFrameIndex]; self.shouldAnimate = NO; self.isProgressive = NO; self.maxBufferCount = 0; self.animatedImageScale = 1; [_fetchQueue cancelAllOperations]; - _fetchQueue = nil; - SD_LOCK(self.lock); - [_frameBuffer removeAllObjects]; - _frameBuffer = nil; - SD_UNLOCK(self.lock); + // clear buffer cache + [self clearFrameBuffer]; } - (void)resetProgressiveImage @@ -179,6 +173,23 @@ static NSUInteger SDDeviceFreeMemory() { // preserve buffer cache } +- (void)resetCurrentFrameIndex +{ + self.currentFrame = nil; + self.currentFrameIndex = 0; + self.currentLoopCount = 0; + self.currentTime = 0; + self.bufferMiss = NO; +} + +- (void)clearFrameBuffer +{ + SD_LOCK(self.lock); + [_frameBuffer removeAllObjects]; + _frameBuffer = nil; + SD_UNLOCK(self.lock); +} + #pragma mark - Accessors #pragma mark Public @@ -466,6 +477,12 @@ static NSUInteger SDDeviceFreeMemory() { #else _displayLink.paused = YES; #endif + if (self.resetFrameIndexWhenStopped) { + [self resetCurrentFrameIndex]; + } + if (self.clearBufferWhenStopped) { + [self clearFrameBuffer]; + } } else { #if SD_UIKIT [super stopAnimating];