Authored by Bogdan Poplauschi

Pass image URL in completion blocks - step 1:

- deprecated block type `SDWebImageCompletedWithFinishedBlock`, replaced with `SDWebImageCompletionWithFinishedBlock` that contains NSURL* param
- deprecated SDWebImageManager `-downloadWithURL:options:progress:completed:` method. Replaced with `downloadImageWithURL:options:progress:completed:` that uses the `SDWebImageCompletionWithFinishedBlock ` as completion block type
- created Deprecated category for SDWebImageManager containing the old method
- replaced the usages of the deprecated items with the new ones
@@ -48,7 +48,7 @@ static char operationKey; @@ -48,7 +48,7 @@ static char operationKey;
48 48
49 if (url) { 49 if (url) {
50 __weak MKAnnotationView *wself = self; 50 __weak MKAnnotationView *wself = self;
51 - id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { 51 + id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
52 if (!wself) return; 52 if (!wself) return;
53 dispatch_main_sync_safe(^{ 53 dispatch_main_sync_safe(^{
54 __strong MKAnnotationView *sself = wself; 54 __strong MKAnnotationView *sself = wself;
@@ -79,7 +79,7 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) { @@ -79,7 +79,7 @@ typedef NS_OPTIONS(NSUInteger, SDWebImageOptions) {
79 79
80 typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType); 80 typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType);
81 81
82 -typedef void(^SDWebImageCompletedWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished); 82 +typedef void(^SDWebImageCompletionWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL);
83 83
84 84
85 @class SDWebImageManager; 85 @class SDWebImageManager;
@@ -126,7 +126,7 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; @@ -126,7 +126,7 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
126 [manager downloadWithURL:imageURL 126 [manager downloadWithURL:imageURL
127 options:0 127 options:0
128 progress:nil 128 progress:nil
129 - completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { 129 + completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
130 if (image) { 130 if (image) {
131 // do something with image 131 // do something with image
132 } 132 }
@@ -188,10 +188,10 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; @@ -188,10 +188,10 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
188 * 188 *
189 * @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation 189 * @return Returns an NSObject conforming to SDWebImageOperation. Should be an instance of SDWebImageDownloaderOperation
190 */ 190 */
191 -- (id <SDWebImageOperation>)downloadWithURL:(NSURL *)url  
192 - options:(SDWebImageOptions)options  
193 - progress:(SDWebImageDownloaderProgressBlock)progressBlock  
194 - completed:(SDWebImageCompletedWithFinishedBlock)completedBlock; 191 +- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url
  192 + options:(SDWebImageOptions)options
  193 + progress:(SDWebImageDownloaderProgressBlock)progressBlock
  194 + completed:(SDWebImageCompletionWithFinishedBlock)completedBlock;
195 195
196 /** 196 /**
197 * Saves image to cache for given URL 197 * Saves image to cache for given URL
@@ -225,3 +225,23 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; @@ -225,3 +225,23 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
225 - (NSString *)cacheKeyForURL:(NSURL *)url; 225 - (NSString *)cacheKeyForURL:(NSURL *)url;
226 226
227 @end 227 @end
  228 +
  229 +
  230 +#pragma mark - Deprecated
  231 +
  232 +typedef void(^SDWebImageCompletedWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) __deprecated_msg("Block type deprecated. Use `SDWebImageCompletionWithFinishedBlock`");
  233 +
  234 +
  235 +@interface SDWebImageManager (Deprecated)
  236 +
  237 +/**
  238 + * Downloads the image at the given URL if not present in cache or return the cached version otherwise.
  239 + *
  240 + * @deprecated This method has been deprecated. Use `downloadImageWithURL:options:progress:completed:`
  241 + */
  242 +- (id <SDWebImageOperation>)downloadWithURL:(NSURL *)url
  243 + options:(SDWebImageOptions)options
  244 + progress:(SDWebImageDownloaderProgressBlock)progressBlock
  245 + completed:(SDWebImageCompletedWithFinishedBlock)completedBlock __deprecated_msg("Method deprecated. Use `downloadImageWithURL:options:progress:completed:`");
  246 +
  247 +@end
@@ -71,7 +71,10 @@ @@ -71,7 +71,10 @@
71 return [self.imageCache diskImageExistsWithKey:key]; 71 return [self.imageCache diskImageExistsWithKey:key];
72 } 72 }
73 73
74 -- (id <SDWebImageOperation>)downloadWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedWithFinishedBlock)completedBlock { 74 +- (id <SDWebImageOperation>)downloadImageWithURL:(NSURL *)url
  75 + options:(SDWebImageOptions)options
  76 + progress:(SDWebImageDownloaderProgressBlock)progressBlock
  77 + completed:(SDWebImageCompletionWithFinishedBlock)completedBlock {
75 // Invoking this method without a completedBlock is pointless 78 // Invoking this method without a completedBlock is pointless
76 NSParameterAssert(completedBlock); 79 NSParameterAssert(completedBlock);
77 80
@@ -97,7 +100,7 @@ @@ -97,7 +100,7 @@
97 if (!url || (!(options & SDWebImageRetryFailed) && isFailedUrl)) { 100 if (!url || (!(options & SDWebImageRetryFailed) && isFailedUrl)) {
98 dispatch_main_sync_safe(^{ 101 dispatch_main_sync_safe(^{
99 NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil]; 102 NSError *error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorFileDoesNotExist userInfo:nil];
100 - completedBlock(nil, error, SDImageCacheTypeNone, YES); 103 + completedBlock(nil, error, SDImageCacheTypeNone, YES, url);
101 }); 104 });
102 return operation; 105 return operation;
103 } 106 }
@@ -121,7 +124,7 @@ @@ -121,7 +124,7 @@
121 dispatch_main_sync_safe(^{ 124 dispatch_main_sync_safe(^{
122 // If image was found in the cache bug SDWebImageRefreshCached is provided, notify about the cached image 125 // If image was found in the cache bug SDWebImageRefreshCached is provided, notify about the cached image
123 // AND try to re-download it in order to let a chance to NSURLCache to refresh it from server. 126 // AND try to re-download it in order to let a chance to NSURLCache to refresh it from server.
124 - completedBlock(image, nil, cacheType, YES); 127 + completedBlock(image, nil, cacheType, YES, url);
125 }); 128 });
126 } 129 }
127 130
@@ -143,12 +146,12 @@ @@ -143,12 +146,12 @@
143 id <SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { 146 id <SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) {
144 if (weakOperation.isCancelled) { 147 if (weakOperation.isCancelled) {
145 dispatch_main_sync_safe(^{ 148 dispatch_main_sync_safe(^{
146 - completedBlock(nil, nil, SDImageCacheTypeNone, finished); 149 + completedBlock(nil, nil, SDImageCacheTypeNone, finished, url);
147 }); 150 });
148 } 151 }
149 else if (error) { 152 else if (error) {
150 dispatch_main_sync_safe(^{ 153 dispatch_main_sync_safe(^{
151 - completedBlock(nil, error, SDImageCacheTypeNone, finished); 154 + completedBlock(nil, error, SDImageCacheTypeNone, finished, url);
152 }); 155 });
153 156
154 if (error.code != NSURLErrorNotConnectedToInternet && error.code != NSURLErrorCancelled && error.code != NSURLErrorTimedOut) { 157 if (error.code != NSURLErrorNotConnectedToInternet && error.code != NSURLErrorCancelled && error.code != NSURLErrorTimedOut) {
@@ -174,7 +177,7 @@ @@ -174,7 +177,7 @@
174 } 177 }
175 178
176 dispatch_main_sync_safe(^{ 179 dispatch_main_sync_safe(^{
177 - completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished); 180 + completedBlock(transformedImage, nil, SDImageCacheTypeNone, finished, url);
178 }); 181 });
179 }); 182 });
180 } 183 }
@@ -184,7 +187,7 @@ @@ -184,7 +187,7 @@
184 } 187 }
185 188
186 dispatch_main_sync_safe(^{ 189 dispatch_main_sync_safe(^{
187 - completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished); 190 + completedBlock(downloadedImage, nil, SDImageCacheTypeNone, finished, url);
188 }); 191 });
189 } 192 }
190 } 193 }
@@ -205,7 +208,7 @@ @@ -205,7 +208,7 @@
205 } 208 }
206 else if (image) { 209 else if (image) {
207 dispatch_main_sync_safe(^{ 210 dispatch_main_sync_safe(^{
208 - completedBlock(image, nil, cacheType, YES); 211 + completedBlock(image, nil, cacheType, YES, url);
209 }); 212 });
210 @synchronized (self.runningOperations) { 213 @synchronized (self.runningOperations) {
211 [self.runningOperations removeObject:operation]; 214 [self.runningOperations removeObject:operation];
@@ -214,7 +217,7 @@ @@ -214,7 +217,7 @@
214 else { 217 else {
215 // Image not in cache and download disallowed by delegate 218 // Image not in cache and download disallowed by delegate
216 dispatch_main_sync_safe(^{ 219 dispatch_main_sync_safe(^{
217 - completedBlock(nil, nil, SDImageCacheTypeNone, YES); 220 + completedBlock(nil, nil, SDImageCacheTypeNone, YES, url);
218 }); 221 });
219 @synchronized (self.runningOperations) { 222 @synchronized (self.runningOperations) {
220 [self.runningOperations removeObject:operation]; 223 [self.runningOperations removeObject:operation];
@@ -245,6 +248,7 @@ @@ -245,6 +248,7 @@
245 248
246 @end 249 @end
247 250
  251 +
248 @implementation SDWebImageCombinedOperation 252 @implementation SDWebImageCombinedOperation
249 253
250 - (void)setCancelBlock:(void (^)())cancelBlock { 254 - (void)setCancelBlock:(void (^)())cancelBlock {
@@ -269,3 +273,21 @@ @@ -269,3 +273,21 @@
269 } 273 }
270 274
271 @end 275 @end
  276 +
  277 +
  278 +@implementation SDWebImageManager (Deprecated)
  279 +
  280 +// deprecated method, uses the non deprecated method
  281 +// adapter for the completion block
  282 +- (id <SDWebImageOperation>)downloadWithURL:(NSURL *)url options:(SDWebImageOptions)options progress:(SDWebImageDownloaderProgressBlock)progressBlock completed:(SDWebImageCompletedWithFinishedBlock)completedBlock {
  283 + return [self downloadImageWithURL:url
  284 + options:options
  285 + progress:progressBlock
  286 + completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  287 + if (completedBlock) {
  288 + completedBlock(image, error, cacheType, finished);
  289 + }
  290 + }];
  291 +}
  292 +
  293 +@end
@@ -56,7 +56,7 @@ @@ -56,7 +56,7 @@
56 - (void)startPrefetchingAtIndex:(NSUInteger)index { 56 - (void)startPrefetchingAtIndex:(NSUInteger)index {
57 if (index >= self.prefetchURLs.count) return; 57 if (index >= self.prefetchURLs.count) return;
58 self.requestedCount++; 58 self.requestedCount++;
59 - [self.manager downloadWithURL:self.prefetchURLs[index] options:self.options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { 59 + [self.manager downloadImageWithURL:self.prefetchURLs[index] options:self.options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
60 if (!finished) return; 60 if (!finished) return;
61 self.finishedCount++; 61 self.finishedCount++;
62 62
@@ -66,7 +66,7 @@ static char operationKey; @@ -66,7 +66,7 @@ static char operationKey;
66 self.imageURLStorage[@(state)] = url; 66 self.imageURLStorage[@(state)] = url;
67 67
68 __weak UIButton *wself = self; 68 __weak UIButton *wself = self;
69 - id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { 69 + id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
70 if (!wself) return; 70 if (!wself) return;
71 dispatch_main_sync_safe(^{ 71 dispatch_main_sync_safe(^{
72 __strong UIButton *sself = wself; 72 __strong UIButton *sself = wself;
@@ -109,7 +109,7 @@ static char operationKey; @@ -109,7 +109,7 @@ static char operationKey;
109 109
110 if (url) { 110 if (url) {
111 __weak UIButton *wself = self; 111 __weak UIButton *wself = self;
112 - id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { 112 + id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
113 if (!wself) return; 113 if (!wself) return;
114 dispatch_main_sync_safe(^{ 114 dispatch_main_sync_safe(^{
115 __strong UIButton *sself = wself; 115 __strong UIButton *sself = wself;
@@ -34,24 +34,20 @@ static char operationKey; @@ -34,24 +34,20 @@ static char operationKey;
34 34
35 if (url) { 35 if (url) {
36 __weak UIImageView *wself = self; 36 __weak UIImageView *wself = self;
37 - id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url  
38 - options:options  
39 - progress:progressBlock  
40 - completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)  
41 - {  
42 - if (!wself) return;  
43 - dispatch_main_sync_safe (^  
44 - {  
45 - if (!wself) return;  
46 - if (image) {  
47 - wself.highlightedImage = image;  
48 - [wself setNeedsLayout];  
49 - }  
50 - if (completedBlock && finished) {  
51 - completedBlock(image, error, cacheType);  
52 - }  
53 - });  
54 - }]; 37 + id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  38 + if (!wself) return;
  39 + dispatch_main_sync_safe (^
  40 + {
  41 + if (!wself) return;
  42 + if (image) {
  43 + wself.highlightedImage = image;
  44 + [wself setNeedsLayout];
  45 + }
  46 + if (completedBlock && finished) {
  47 + completedBlock(image, error, cacheType);
  48 + }
  49 + });
  50 + }];
55 objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC); 51 objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
56 } 52 }
57 } 53 }
@@ -50,7 +50,7 @@ static char operationArrayKey; @@ -50,7 +50,7 @@ static char operationArrayKey;
50 50
51 if (url) { 51 if (url) {
52 __weak UIImageView *wself = self; 52 __weak UIImageView *wself = self;
53 - id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { 53 + id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
54 if (!wself) return; 54 if (!wself) return;
55 dispatch_main_sync_safe(^{ 55 dispatch_main_sync_safe(^{
56 if (!wself) return; 56 if (!wself) return;
@@ -85,7 +85,7 @@ static char operationArrayKey; @@ -85,7 +85,7 @@ static char operationArrayKey;
85 NSMutableArray *operationsArray = [[NSMutableArray alloc] init]; 85 NSMutableArray *operationsArray = [[NSMutableArray alloc] init];
86 86
87 for (NSURL *logoImageURL in arrayOfURLs) { 87 for (NSURL *logoImageURL in arrayOfURLs) {
88 - id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) { 88 + id <SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadImageWithURL:logoImageURL options:0 progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
89 if (!wself) return; 89 if (!wself) return;
90 dispatch_main_sync_safe(^{ 90 dispatch_main_sync_safe(^{
91 __strong UIImageView *sself = wself; 91 __strong UIImageView *sself = wself;