From 5c94f17a17c1bf6fb557393486bb3875c5320949 Mon Sep 17 00:00:00 2001 From: Matej Bukovinski Date: Fri, 16 Nov 2012 14:54:52 +0100 Subject: [PATCH] Disk cache now uses the raw image data returned from the server. This preserves the image quality and retains some image attributes, such as the alpha channel (for non-jpeg images). --- SDWebImage/SDWebImageDownloader.h | 2 +- SDWebImage/SDWebImageDownloader.m | 8 ++++---- SDWebImage/SDWebImageDownloaderOperation.m | 12 ++++++------ SDWebImage/SDWebImageManager.m | 4 ++-- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/SDWebImage/SDWebImageDownloader.h b/SDWebImage/SDWebImageDownloader.h index 5e1a8bd2..001735fa 100644 --- a/SDWebImage/SDWebImageDownloader.h +++ b/SDWebImage/SDWebImageDownloader.h @@ -20,7 +20,7 @@ extern NSString *const SDWebImageDownloadStartNotification; extern NSString *const SDWebImageDownloadStopNotification; typedef void(^SDWebImageDownloaderProgressBlock)(NSUInteger receivedSize, long long expectedSize); -typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSError *error, BOOL finished); +typedef void(^SDWebImageDownloaderCompletedBlock)(UIImage *image, NSData *data, NSError *error, BOOL finished); /** * Asynchronous downloader dedicated and optimized for image loading. diff --git a/SDWebImage/SDWebImageDownloader.m b/SDWebImage/SDWebImageDownloader.m index be1a9e92..09d79655 100644 --- a/SDWebImage/SDWebImageDownloader.m +++ b/SDWebImage/SDWebImageDownloader.m @@ -91,7 +91,7 @@ NSString *const kCompletedCallbackKey = @"completed"; return _downloadQueue.maxConcurrentOperationCount; } -- (id)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger, long long))progressBlock completed:(void (^)(UIImage *, NSError *, BOOL))completedBlock +- (id)downloadImageWithURL:(NSURL *)url options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger, long long))progressBlock completed:(void (^)(UIImage *, NSData *, NSError *, BOOL))completedBlock { __block SDWebImageDownloaderOperation *operation; __weak SDWebImageDownloader *wself = self; @@ -114,7 +114,7 @@ NSString *const kCompletedCallbackKey = @"completed"; if (callback) callback(receivedSize, expectedSize); } } - completed:^(UIImage *image, NSError *error, BOOL finished) + completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) { if (!wself) return; SDWebImageDownloader *sself = wself; @@ -122,7 +122,7 @@ NSString *const kCompletedCallbackKey = @"completed"; for (NSDictionary *callbacks in callbacksForURL) { SDWebImageDownloaderCompletedBlock callback = callbacks[kCompletedCallbackKey]; - if (callback) callback(image, error, finished); + if (callback) callback(image, data, error, finished); } } cancelled:^ @@ -137,7 +137,7 @@ NSString *const kCompletedCallbackKey = @"completed"; return operation; } -- (void)addProgressCallback:(void (^)(NSUInteger, long long))progressBlock andCompletedBlock:(void (^)(UIImage *, NSError *, BOOL))completedBlock forURL:(NSURL *)url createCallback:(void (^)())createCallback +- (void)addProgressCallback:(void (^)(NSUInteger, long long))progressBlock andCompletedBlock:(void (^)(UIImage *, NSData *data, NSError *, BOOL))completedBlock forURL:(NSURL *)url createCallback:(void (^)())createCallback { dispatch_barrier_sync(self.barrierQueue, ^ { diff --git a/SDWebImage/SDWebImageDownloaderOperation.m b/SDWebImage/SDWebImageDownloaderOperation.m index fce2e7bc..cfafba3a 100644 --- a/SDWebImage/SDWebImageDownloaderOperation.m +++ b/SDWebImage/SDWebImageDownloaderOperation.m @@ -30,7 +30,7 @@ size_t width, height; } -- (id)initWithRequest:(NSURLRequest *)request queue:(dispatch_queue_t)queue options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger, long long))progressBlock completed:(void (^)(UIImage *, NSError *, BOOL))completedBlock cancelled:(void (^)())cancelBlock +- (id)initWithRequest:(NSURLRequest *)request queue:(dispatch_queue_t)queue options:(SDWebImageDownloaderOptions)options progress:(void (^)(NSUInteger, long long))progressBlock completed:(void (^)(UIImage *, NSData *, NSError *, BOOL))completedBlock cancelled:(void (^)())cancelBlock { if ((self = [super init])) { @@ -77,7 +77,7 @@ { if (self.completedBlock) { - self.completedBlock(nil, [NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Connection can't be initialized"}], YES); + self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:0 userInfo:@{NSLocalizedDescriptionKey: @"Connection can't be initialized"}], YES); } } }); @@ -158,7 +158,7 @@ if (self.completedBlock) { - self.completedBlock(nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES); + self.completedBlock(nil, nil, [NSError errorWithDomain:NSURLErrorDomain code:[((NSHTTPURLResponse *)response) statusCode] userInfo:nil], YES); } [self done]; @@ -228,7 +228,7 @@ { UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, [UIImage imageWithCGImage:partialImageRef])]; CGImageRelease(partialImageRef); - if (self.completedBlock) self.completedBlock(image, nil, NO); + if (self.completedBlock) self.completedBlock(image, nil, nil, NO); } } @@ -251,7 +251,7 @@ UIImage *image = [UIImage decodedImageWithImage:SDScaledImageForPath(self.request.URL.absoluteString, self.imageData)]; dispatch_async(dispatch_get_main_queue(), ^ { - completionBlock(image, nil, YES); + completionBlock(image, self.imageData, nil, YES); completionBlock = nil; }); } @@ -266,7 +266,7 @@ if (self.completedBlock) { - self.completedBlock(nil, error, YES); + self.completedBlock(nil, nil, error, YES); } [self done]; diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 4e3e6a4c..98bad35b 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -97,7 +97,7 @@ 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, NSError *error, BOOL finished) + __block id subOperation = [self.imageDownloader downloadImageWithURL:url options:downloaderOptions progress:progressBlock completed:^(UIImage *downloadedImage, NSData *data, NSError *error, BOOL finished) { dispatch_async(dispatch_get_main_queue(), ^ { @@ -109,7 +109,7 @@ } else if (downloadedImage && finished) { - [self.imageCache storeImage:downloadedImage forKey:key]; + [self.imageCache storeImage:downloadedImage imageData:data forKey:key toDisk:YES]; } if (finished)