Authored by Matej Bukovinski
Committed by Olivier Poitrey

Implemented progress callbacks and related fixes.

@@ -13,8 +13,8 @@ @@ -13,8 +13,8 @@
13 NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification"; 13 NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
14 NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification"; 14 NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
15 15
16 -NSString *const kProgressCallbackKey = @"completed";  
17 -NSString *const kCompletedCallbackKey = @"completed"; 16 +static NSString *const kProgressCallbackKey = @"progress";
  17 +static NSString *const kCompletedCallbackKey = @"completed";
18 18
19 @interface SDWebImageDownloader () 19 @interface SDWebImageDownloader ()
20 20
@@ -107,7 +107,7 @@ NSString *const kCompletedCallbackKey = @"completed"; @@ -107,7 +107,7 @@ NSString *const kCompletedCallbackKey = @"completed";
107 { 107 {
108 if (!wself) return; 108 if (!wself) return;
109 SDWebImageDownloader *sself = wself; 109 SDWebImageDownloader *sself = wself;
110 - NSArray *callbacksForURL = [sself callbacksForURL:url remove:YES]; 110 + NSArray *callbacksForURL = [sself callbacksForURL:url remove:NO];
111 for (NSDictionary *callbacks in callbacksForURL) 111 for (NSDictionary *callbacks in callbacksForURL)
112 { 112 {
113 SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey]; 113 SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];
@@ -148,6 +148,10 @@ @@ -148,6 +148,10 @@
148 { 148 {
149 self.expectedSize = response.expectedContentLength > 0 ? (NSUInteger)response.expectedContentLength : 0; 149 self.expectedSize = response.expectedContentLength > 0 ? (NSUInteger)response.expectedContentLength : 0;
150 self.imageData = [NSMutableData.alloc initWithCapacity:self.expectedSize]; 150 self.imageData = [NSMutableData.alloc initWithCapacity:self.expectedSize];
  151 + if (self.progressBlock)
  152 + {
  153 + self.progressBlock(0, self.expectedSize);
  154 + }
151 }); 155 });
152 } 156 }
153 else 157 else
@@ -234,6 +238,10 @@ @@ -234,6 +238,10 @@
234 238
235 CFRelease(imageSource); 239 CFRelease(imageSource);
236 } 240 }
  241 + if (self.progressBlock)
  242 + {
  243 + self.progressBlock(self.imageData.length, self.expectedSize);
  244 + }
237 }); 245 });
238 } 246 }
239 247
@@ -97,7 +97,14 @@ @@ -97,7 +97,14 @@
97 SDWebImageDownloaderOptions downloaderOptions = 0; 97 SDWebImageDownloaderOptions downloaderOptions = 0;
98 if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority; 98 if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority;
99 if (options & SDWebImageProgressiveDownload) downloaderOptions |= SDWebImageDownloaderProgressiveDownload; 99 if (options & SDWebImageProgressiveDownload) downloaderOptions |= SDWebImageDownloaderProgressiveDownload;
100 - __block id<SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) 100 + __block id<SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:^(NSUInteger receivedSize, long long expectedSize)
  101 + {
  102 + dispatch_async(dispatch_get_main_queue(), ^
  103 + {
  104 + progressBlock(receivedSize, expectedSize);
  105 + });
  106 + }
  107 + completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished)
101 { 108 {
102 dispatch_async(dispatch_get_main_queue(), ^ 109 dispatch_async(dispatch_get_main_queue(), ^
103 { 110 {