From aecb13d4210f5a91669fa5003e538f47dd8f05cb Mon Sep 17 00:00:00 2001 From: Bogdan Poplauschi Date: Wed, 1 Jun 2016 09:40:36 +0300 Subject: [PATCH] Extended the SDWebImageManager `loadImageWithURL:options:progress:completed:` so it also returns the NSData (we will need it later for the GIF images). - had to add an NSData param to `SDWebImageCompletionWithFinishedBlock `, so to make it simpler see this change for users, renamed the block type to `SDInternalCompletionBlock` - pass the NSData from the ImageCache or the Downloader - updated all classes using this method with the new signature --- SDWebImage/MKAnnotationView+WebCache.m | 2 +- SDWebImage/SDWebImageManager.h | 4 ++-- SDWebImage/SDWebImageManager.m | 20 ++++++++++---------- SDWebImage/SDWebImagePrefetcher.m | 2 +- SDWebImage/UIButton+WebCache.m | 4 ++-- SDWebImage/UIImageView+HighlightedWebCache.m | 2 +- SDWebImage/UIImageView+WebCache.m | 4 ++-- 7 files changed, 19 insertions(+), 19 deletions(-) diff --git a/SDWebImage/MKAnnotationView+WebCache.m b/SDWebImage/MKAnnotationView+WebCache.m index 7e982ede..171880fa 100644 --- a/SDWebImage/MKAnnotationView+WebCache.m +++ b/SDWebImage/MKAnnotationView+WebCache.m @@ -46,7 +46,7 @@ static char imageURLKey; if (url) { __weak __typeof(self)wself = self; - id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { + id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (!wself) return; dispatch_main_sync_safe(^{ __strong MKAnnotationView *sself = wself; diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index 7edfc74c..95224d44 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -92,7 +92,7 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) { typedef void(^SDWebImageCompletionBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL); -typedef void(^SDWebImageCompletionWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL); +typedef void(^SDInternalCompletionBlock)(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL); typedef NSString *(^SDWebImageCacheKeyFilterBlock)(NSURL *url); @@ -212,7 +212,7 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; - (id )loadImageWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock - completed:(SDWebImageCompletionWithFinishedBlock)completedBlock; + completed:(SDInternalCompletionBlock)completedBlock; /** * Saves image to cache for given URL diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 8004fad9..20bcb65c 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -116,9 +116,9 @@ } - (id )loadImageWithURL:(NSURL *)url - options:(SDWebImageOptions)options - progress:(SDWebImageDownloaderProgressBlock)progressBlock - completed:(SDWebImageCompletionWithFinishedBlock)completedBlock { + options:(SDWebImageOptions)options + progress:(SDWebImageDownloaderProgressBlock)progressBlock + completed:(SDInternalCompletionBlock)completedBlock { // Invoking this method without a completedBlock is pointless NSAssert(completedBlock != nil, @"If you mean to prefetch the image, use -[SDWebImagePrefetcher prefetchURLs] instead"); @@ -144,7 +144,7 @@ if (url.absoluteString.length == 0 || (!(options & SDWebImageRetryFailed) && isFailedUrl)) { dispatch_main_sync_safe(^{ NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]; - completedBlock(nil, error, SDImageCacheTypeNone, YES, url); + completedBlock(nil, nil, error, SDImageCacheTypeNone, YES, url); }); return operation; } @@ -168,7 +168,7 @@ dispatch_main_sync_safe(^{ // If image was found in the cache but SDWebImageRefreshCached is provided, notify about the cached image // AND try to re-download it in order to let a chance to NSURLCache to refresh it from server. - completedBlock(cachedImage, nil, cacheType, YES, url); + completedBlock(cachedImage, cachedData, nil, cacheType, YES, url); }); } @@ -196,7 +196,7 @@ } else if (error) { dispatch_main_sync_safe(^{ if (strongOperation && !strongOperation.isCancelled) { - completedBlock(nil, error, SDImageCacheTypeNone, finished, url); + completedBlock(nil, nil, error, SDImageCacheTypeNone, finished, url); } }); @@ -234,7 +234,7 @@ dispatch_main_sync_safe(^{ if (strongOperation && !strongOperation.isCancelled) { - completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished, url); + completedBlock(transformedImage, downloadedData, nil, SDImageCacheTypeNone, finished, url); } }); }); @@ -245,7 +245,7 @@ dispatch_main_sync_safe(^{ if (strongOperation && !strongOperation.isCancelled) { - completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished, url); + completedBlock(downloadedImage, downloadedData, nil, SDImageCacheTypeNone, finished, url); } }); } @@ -273,7 +273,7 @@ dispatch_main_sync_safe(^{ __strong __typeof(weakOperation) strongOperation = weakOperation; if (strongOperation && !strongOperation.isCancelled) { - completedBlock(cachedImage, nil, cacheType, YES, url); + completedBlock(cachedImage, cachedData, nil, cacheType, YES, url); } }); @synchronized (self.runningOperations) { @@ -284,7 +284,7 @@ dispatch_main_sync_safe(^{ __strong __typeof(weakOperation) strongOperation = weakOperation; if (strongOperation && !weakOperation.isCancelled) { - completedBlock(nil, nil, SDImageCacheTypeNone, YES, url); + completedBlock(nil, nil, nil, SDImageCacheTypeNone, YES, url); } }); @synchronized (self.runningOperations) { diff --git a/SDWebImage/SDWebImagePrefetcher.m b/SDWebImage/SDWebImagePrefetcher.m index 1dfddafd..b7876624 100644 --- a/SDWebImage/SDWebImagePrefetcher.m +++ b/SDWebImage/SDWebImagePrefetcher.m @@ -57,7 +57,7 @@ - (void)startPrefetchingAtIndex:(NSUInteger)index { if (index >= self.prefetchURLs.count) return; self.requestedCount++; - [self.manager loadImageWithURL:self.prefetchURLs[index] options:self.options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { + [self.manager loadImageWithURL:self.prefetchURLs[index] options:self.options progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (!finished) return; self.finishedCount++; diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index fd296506..10a1c066 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -69,7 +69,7 @@ static char imageURLStorageKey; self.imageURLStorage[@(state)] = url; __weak __typeof(self)wself = self; - id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { + id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (!wself) return; dispatch_main_sync_safe(^{ __strong UIButton *sself = wself; @@ -117,7 +117,7 @@ static char imageURLStorageKey; if (url) { __weak __typeof(self)wself = self; - id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { + id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (!wself) return; dispatch_main_sync_safe(^{ __strong UIButton *sself = wself; diff --git a/SDWebImage/UIImageView+HighlightedWebCache.m b/SDWebImage/UIImageView+HighlightedWebCache.m index 9cc10252..716b37f2 100644 --- a/SDWebImage/UIImageView+HighlightedWebCache.m +++ b/SDWebImage/UIImageView+HighlightedWebCache.m @@ -34,7 +34,7 @@ if (url) { __weak __typeof(self)wself = self; - id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { + id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (!wself) return; dispatch_main_sync_safe (^ { diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index a86e4c06..616cc2da 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -59,7 +59,7 @@ static char TAG_ACTIVITY_SHOW; } __weak __typeof(self)wself = self; - id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { + id operation = [SDWebImageManager.sharedManager loadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { [wself removeActivityIndicator]; if (!wself) return; dispatch_main_sync_safe(^{ @@ -113,7 +113,7 @@ static char TAG_ACTIVITY_SHOW; NSMutableArray *operationsArray = [[NSMutableArray alloc] init]; for (NSURL *logoImageURL in arrayOfURLs) { - id operation = [SDWebImageManager.sharedManager loadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { + id operation = [SDWebImageManager.sharedManager loadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSData *data, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) { if (!wself) return; dispatch_main_sync_safe(^{ __strong UIImageView *sself = wself;