From 733f465e5c16939eab5ee050b9d696545234f6f2 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Fri, 7 Sep 2018 22:44:21 +0800 Subject: [PATCH] Add autoreleasepool to release autorelease objects in advance when using GCD for 4.x --- SDWebImage/SDWebImageDownloaderOperation.m | 76 ++++++++++++---------- SDWebImage/SDWebImageManager.m | 34 +++++----- 2 files changed, 59 insertions(+), 51 deletions(-) diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index 722645f0..e0a68976 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -345,17 +345,19 @@ didReceiveResponse:(NSURLResponse *)response // progressive decode the image in coder queue dispatch_async(self.coderQueue, ^{ - UIImage *image = [self.progressiveCoder incrementallyDecodedImageWithData:imageData finished:finished]; - if (image) { - NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; - image = [self scaledImageForKey:key image:image]; - if (self.shouldDecompressImages) { - image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}]; + @autoreleasepool { + UIImage *image = [self.progressiveCoder incrementallyDecodedImageWithData:imageData finished:finished]; + if (image) { + NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; + image = [self scaledImageForKey:key image:image]; + if (self.shouldDecompressImages) { + image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(NO)}]; + } + + // 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. + + [self callCompletionBlocksWithImage:image imageData:nil error:nil finished:NO]; } - - // 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. - - [self callCompletionBlocksWithImage:image imageData:nil error:nil finished:NO]; } }); } @@ -416,36 +418,38 @@ didReceiveResponse:(NSURLResponse *)response } else { // decode the image in coder queue dispatch_async(self.coderQueue, ^{ - UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:imageData]; - NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; - image = [self scaledImageForKey:key image:image]; - - BOOL shouldDecode = YES; - // Do not force decoding animated GIFs and WebPs - if (image.images) { - shouldDecode = NO; - } else { -#ifdef SD_WEBP - SDImageFormat imageFormat = [NSData sd_imageFormatForImageData:imageData]; - if (imageFormat == SDImageFormatWebP) { + @autoreleasepool { + UIImage *image = [[SDWebImageCodersManager sharedInstance] decodedImageWithData:imageData]; + NSString *key = [[SDWebImageManager sharedManager] cacheKeyForURL:self.request.URL]; + image = [self scaledImageForKey:key image:image]; + + BOOL shouldDecode = YES; + // Do not force decoding animated GIFs and WebPs + if (image.images) { shouldDecode = NO; - } + } else { +#ifdef SD_WEBP + SDImageFormat imageFormat = [NSData sd_imageFormatForImageData:imageData]; + if (imageFormat == SDImageFormatWebP) { + shouldDecode = NO; + } #endif - } - - if (shouldDecode) { - if (self.shouldDecompressImages) { - BOOL shouldScaleDown = self.options & SDWebImageDownloaderScaleDownLargeImages; - image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(shouldScaleDown)}]; } + + if (shouldDecode) { + if (self.shouldDecompressImages) { + BOOL shouldScaleDown = self.options & SDWebImageDownloaderScaleDownLargeImages; + image = [[SDWebImageCodersManager sharedInstance] decompressedImageWithImage:image data:&imageData options:@{SDWebImageCoderScaleDownLargeImagesKey: @(shouldScaleDown)}]; + } + } + CGSize imageSize = image.size; + if (imageSize.width == 0 || imageSize.height == 0) { + [self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}]]; + } else { + [self callCompletionBlocksWithImage:image imageData:imageData error:nil finished:YES]; + } + [self done]; } - CGSize imageSize = image.size; - if (imageSize.width == 0 || imageSize.height == 0) { - [self callCompletionBlocksWithError:[NSError errorWithDomain:SDWebImageErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey : @"Downloaded image has 0 pixels"}]]; - } else { - [self callCompletionBlocksWithImage:image imageData:imageData error:nil finished:YES]; - } - [self done]; }); } } else { diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 24da020e..c912fb5f 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -243,28 +243,32 @@ // Image refresh hit the NSURLCache cache, do not call the completion block } else if (downloadedImage && (!downloadedImage.images || (options & SDWebImageTransformAnimatedImage)) && [self.delegate respondsToSelector:@selector(imageManager:transformDownloadedImage:withURL:)]) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ - UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url]; - - if (transformedImage && finished) { - BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage]; - NSData *cacheData; - // pass nil if the image was transformed, so we can recalculate the data from the image - if (self.cacheSerializer) { - cacheData = self.cacheSerializer(transformedImage, (imageWasTransformed ? nil : downloadedData), url); - } else { - cacheData = (imageWasTransformed ? nil : downloadedData); + @autoreleasepool { + UIImage *transformedImage = [self.delegate imageManager:self transformDownloadedImage:downloadedImage withURL:url]; + + if (transformedImage && finished) { + BOOL imageWasTransformed = ![transformedImage isEqual:downloadedImage]; + NSData *cacheData; + // pass nil if the image was transformed, so we can recalculate the data from the image + if (self.cacheSerializer) { + cacheData = self.cacheSerializer(transformedImage, (imageWasTransformed ? nil : downloadedData), url); + } else { + cacheData = (imageWasTransformed ? nil : downloadedData); + } + [self.imageCache storeImage:transformedImage imageData:cacheData forKey:key toDisk:cacheOnDisk completion:nil]; } - [self.imageCache storeImage:transformedImage imageData:cacheData forKey:key toDisk:cacheOnDisk completion:nil]; + + [self callCompletionBlockForOperation:strongSubOperation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url]; } - - [self callCompletionBlockForOperation:strongSubOperation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url]; }); } else { if (downloadedImage && finished) { if (self.cacheSerializer) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ - NSData *cacheData = self.cacheSerializer(downloadedImage, downloadedData, url); - [self.imageCache storeImage:downloadedImage imageData:cacheData forKey:key toDisk:cacheOnDisk completion:nil]; + @autoreleasepool { + NSData *cacheData = self.cacheSerializer(downloadedImage, downloadedData, url); + [self.imageCache storeImage:downloadedImage imageData:cacheData forKey:key toDisk:cacheOnDisk completion:nil]; + } }); } else { [self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key toDisk:cacheOnDisk completion:nil];