Add autoreleasepool to release autorelease objects in advance when using GCD

This commit is contained in:
zhongwuzw 2018-09-07 10:48:38 +08:00
parent f22a69891c
commit 703631d51a
2 changed files with 36 additions and 28 deletions

View File

@ -361,12 +361,14 @@ didReceiveResponse:(NSURLResponse *)response
// progressive decode the image in coder queue // progressive decode the image in coder queue
dispatch_async(self.coderQueue, ^{ dispatch_async(self.coderQueue, ^{
@autoreleasepool {
UIImage *image = SDImageLoaderDecodeProgressiveImageData(imageData, self.request.URL, finished, self, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context); UIImage *image = SDImageLoaderDecodeProgressiveImageData(imageData, self.request.URL, finished, self, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context);
if (image) { 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. // 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]; [self callCompletionBlocksWithImage:image imageData:nil error:nil finished:NO];
} }
}
}); });
} }
@ -428,6 +430,7 @@ didReceiveResponse:(NSURLResponse *)response
} else { } else {
// decode the image in coder queue // decode the image in coder queue
dispatch_async(self.coderQueue, ^{ dispatch_async(self.coderQueue, ^{
@autoreleasepool {
UIImage *image = SDImageLoaderDecodeImageData(imageData, self.request.URL, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context); UIImage *image = SDImageLoaderDecodeImageData(imageData, self.request.URL, [[self class] imageOptionsFromDownloaderOptions:self.options], self.context);
CGSize imageSize = image.size; CGSize imageSize = image.size;
if (imageSize.width == 0 || imageSize.height == 0) { if (imageSize.width == 0 || imageSize.height == 0) {
@ -436,6 +439,7 @@ didReceiveResponse:(NSURLResponse *)response
[self callCompletionBlocksWithImage:image imageData:imageData error:nil finished:YES]; [self callCompletionBlocksWithImage:image imageData:imageData error:nil finished:YES];
} }
[self done]; [self done];
}
}); });
} }
} else { } else {

View File

@ -282,6 +282,7 @@ static id<SDImageLoader> _defaultImageLoader;
id<SDWebImageCacheSerializer> cacheSerializer = context[SDWebImageContextCacheSerializer]; id<SDWebImageCacheSerializer> cacheSerializer = context[SDWebImageContextCacheSerializer];
if (downloadedImage && (!downloadedImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)) && transformer) { if (downloadedImage && (!downloadedImage.sd_isAnimated || (options & SDWebImageTransformAnimatedImage)) && transformer) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
@autoreleasepool {
UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key]; UIImage *transformedImage = [transformer transformedImageWithImage:downloadedImage forKey:key];
if (transformedImage && finished) { if (transformedImage && finished) {
NSString *transformerKey = [transformer transformerKey]; NSString *transformerKey = [transformer transformerKey];
@ -298,13 +299,16 @@ static id<SDImageLoader> _defaultImageLoader;
} }
[self callCompletionBlockForOperation:strongOperation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url]; [self callCompletionBlockForOperation:strongOperation completion:completedBlock image:transformedImage data:downloadedData error:nil cacheType:SDImageCacheTypeNone finished:finished url:url];
}
}); });
} else { } else {
if (downloadedImage && finished) { if (downloadedImage && finished) {
if (cacheSerializer) { if (cacheSerializer) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
@autoreleasepool {
NSData *cacheData = [cacheSerializer cacheDataWithImage:downloadedImage originalData:downloadedData imageURL:url]; NSData *cacheData = [cacheSerializer cacheDataWithImage:downloadedImage originalData:downloadedData imageURL:url];
[self.imageCache storeImage:downloadedImage imageData:cacheData forKey:key cacheType:storeCacheType completion:nil]; [self.imageCache storeImage:downloadedImage imageData:cacheData forKey:key cacheType:storeCacheType completion:nil];
}
}); });
} else { } else {
[self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key cacheType:storeCacheType completion:nil]; [self.imageCache storeImage:downloadedImage imageData:downloadedData forKey:key cacheType:storeCacheType completion:nil];