Implemented progress callbacks and related fixes.

This commit is contained in:
Matej Bukovinski 2012-11-19 11:26:20 +01:00 committed by Olivier Poitrey
parent 2e8c02556a
commit ab185ea6e8
3 changed files with 19 additions and 4 deletions

View File

@ -13,8 +13,8 @@
NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification"; NSString *const SDWebImageDownloadStartNotification = @"SDWebImageDownloadStartNotification";
NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification"; NSString *const SDWebImageDownloadStopNotification = @"SDWebImageDownloadStopNotification";
NSString *const kProgressCallbackKey = @"completed"; static NSString *const kProgressCallbackKey = @"progress";
NSString *const kCompletedCallbackKey = @"completed"; static NSString *const kCompletedCallbackKey = @"completed";
@interface SDWebImageDownloader () @interface SDWebImageDownloader ()
@ -107,7 +107,7 @@ NSString *const kCompletedCallbackKey = @"completed";
{ {
if (!wself) return; if (!wself) return;
SDWebImageDownloader *sself = wself; SDWebImageDownloader *sself = wself;
NSArray *callbacksForURL = [sself callbacksForURL:url remove:YES]; NSArray *callbacksForURL = [sself callbacksForURL:url remove:NO];
for (NSDictionary *callbacks in callbacksForURL) for (NSDictionary *callbacks in callbacksForURL)
{ {
SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey]; SDWebImageDownloaderProgressBlock callback = callbacks[kProgressCallbackKey];

View File

@ -148,6 +148,10 @@
{ {
self.expectedSize = response.expectedContentLength > 0 ? (NSUInteger)response.expectedContentLength : 0; self.expectedSize = response.expectedContentLength > 0 ? (NSUInteger)response.expectedContentLength : 0;
self.imageData = [NSMutableData.alloc initWithCapacity:self.expectedSize]; self.imageData = [NSMutableData.alloc initWithCapacity:self.expectedSize];
if (self.progressBlock)
{
self.progressBlock(0, self.expectedSize);
}
}); });
} }
else else
@ -234,6 +238,10 @@
CFRelease(imageSource); CFRelease(imageSource);
} }
if (self.progressBlock)
{
self.progressBlock(self.imageData.length, self.expectedSize);
}
}); });
} }

View File

@ -97,7 +97,14 @@
SDWebImageDownloaderOptions downloaderOptions = 0; SDWebImageDownloaderOptions downloaderOptions = 0;
if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority; if (options & SDWebImageLowPriority) downloaderOptions |= SDWebImageDownloaderLowPriority;
if (options & SDWebImageProgressiveDownload) downloaderOptions |= SDWebImageDownloaderProgressiveDownload; if (options & SDWebImageProgressiveDownload) downloaderOptions |= SDWebImageDownloaderProgressiveDownload;
__block id<SDWebImageOperation> subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) __block id<SDWebImageOperation> 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(), ^ dispatch_async(dispatch_get_main_queue(), ^
{ {