From 30618ce37dd9644e110b493c4a0569791979e4c7 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 5 Mar 2021 10:51:00 +0800 Subject: [PATCH] Avoid the strong retain during download decoding, this can make the download opearation fast to destroy and reduce memory peak, especially in progressive decoding --- SDWebImage/Core/SDWebImageDownloaderOperation.m | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/SDWebImage/Core/SDWebImageDownloaderOperation.m b/SDWebImage/Core/SDWebImageDownloaderOperation.m index c0f269e0..1fbff7ba 100644 --- a/SDWebImage/Core/SDWebImageDownloaderOperation.m +++ b/SDWebImage/Core/SDWebImageDownloaderOperation.m @@ -11,7 +11,6 @@ #import "SDInternalMacros.h" #import "SDWebImageDownloaderResponseModifier.h" #import "SDWebImageDownloaderDecryptor.h" -#import "SDAnimatedImage.h" static NSString *const kProgressCallbackKey = @"progress"; static NSString *const kCompletedCallbackKey = @"completed"; @@ -395,7 +394,12 @@ didReceiveResponse:(NSURLResponse *)response // keep maximum one progressive decode process during download if (self.coderQueue.operationCount == 0) { // NSOperation have autoreleasepool, don't need to create extra one + @weakify(self); [self.coderQueue addOperationWithBlock:^{ + @strongify(self); + if (!self) { + return; + } UIImage *image = SDImageLoaderDecodeProgressiveImageData(imageData, self.request.URL, NO, self, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context); if (image) { // We do not keep the progressive decoding image even when `finished`=YES. Because they are for view rendering but not take full function from downloader options. And some coders implementation may not keep consistent between progressive decoding and normal decoding. @@ -472,7 +476,12 @@ didReceiveResponse:(NSURLResponse *)response } else { // decode the image in coder queue, cancel all previous decoding process [self.coderQueue cancelAllOperations]; + @weakify(self); [self.coderQueue addOperationWithBlock:^{ + @strongify(self); + if (!self) { + return; + } // check if we already use progressive decoding, use that to produce faster decoding id progressiveCoder = SDImageLoaderGetProgressiveCoder(self); UIImage *image;