From ab185ea6e8e42a0f69f719ce5c9cb368276b5476 Mon Sep 17 00:00:00 2001 From: Matej Bukovinski Date: Mon, 19 Nov 2012 11:26:20 +0100 Subject: [PATCH] Implemented progress callbacks and related fixes. --- SDWebImage/SDWebImageDownloader.m | 6 +++--- SDWebImage/SDWebImageDownloaderOperation.m | 8 ++++++++ SDWebImage/SDWebImageManager.m | 9 ++++++++- 3 files changed, 19 insertions(+), 4 deletions(-) diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index 09d79655..ea446909 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -13,8 +13,8 @@ NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification"; NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification"; -NSString *const kProgressCallbackKey = @"completed"; -NSString *const kCompletedCallbackKey = @"completed"; +static NSString *const kProgressCallbackKey = @"progress"; +static NSString *const kCompletedCallbackKey = @"completed"; @interface SDWebImageDownloader () @@ -107,7 +107,7 @@ NSString *const kCompletedCallbackKey = @"completed"; { if (!wself) return; SDWebImageDownloader *sself = wself; - NSArray *callbacksForURL = [sself callbacksForURL:url remove:YES]; + NSArray *callbacksForURL = [sself callbacksForURL:url remove:NO]; for (NSDictionary *callbacks in callbacksForURL) { SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey]; diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index cfafba3a..7996b161 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -148,6 +148,10 @@ { self.expectedSize = response.expectedContentLength > 0 ? (NSUInteger)response.expectedContentLength : 0; self.imageData = [NSMutableData.alloc initWithCapacity:self.expectedSize]; + if (self.progressBlock) + { + self.progressBlock(0, self.expectedSize); + } }); } else @@ -234,6 +238,10 @@ CFRelease(imageSource); } + if (self.progressBlock) + { + self.progressBlock(self.imageData.length, self.expectedSize); + } }); } diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 380ea190..314067fe 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -97,7 +97,14 @@ SDWebImageDownloaderOptions downloaderOptions = 0; if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority; if (options & SDWebImageProgressiveDownload) downloaderOptions |= SDWebImageDownloaderProgressiveDownload; - __block id subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) + __block id subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:^(NSUInteger receivedSize, long long expectedSize) + { + dispatch_async(dispatch_get_main_queue(), ^ + { + progressBlock(receivedSize, expectedSize); + }); + } + completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { dispatch_async(dispatch_get_main_queue(), ^ {