Passing the exact cache type in the completion block.

- none, disk and memory
- can still be used as a bool (if true there was a cache hit)
This commit is contained in:
Matej Bukovinski 2012-11-16 16:05:43 +01:00 committed by Olivier Poitrey
parent 5c94f17a17
commit 2e8c02556a
8 changed files with 37 additions and 20 deletions

View File

@ -72,7 +72,7 @@ has completed with success or not:
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:@"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:@"placeholder.png"]
completed:^(UIImage *image, NSError *error, BOOL fromCache) {... completion code here ...}];
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType) {... completion code here ...}];
```
Note: neither your success nor failure block will be call if your image request is canceled before completion.
@ -93,7 +93,7 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager];
{
// progression tracking code
}
completed:^(UIImage *image, NSError *error, BOOL fromCache)
completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType)
{
if (image)
{

View File

@ -9,6 +9,23 @@
#import <Foundation/Foundation.h>
#import "SDWebImageCompat.h"
enum SDImageCacheType
{
/**
* The image wasn't available the SDWebImage caches, but was downloaded from the web.
*/
SDImageCacheTypeNone = 0,
/**
* The image was obtained from the disk cache.
*/
SDImageCacheTypeDisk,
/**
* The image was obtained from the disk cache.
*/
SDImageCacheTypeMemory
};
typedef enum SDImageCacheType SDImageCacheType;
/**
* SDImageCache maintains a memory cache and an optional disk cache. Disk cache write operations are performed
* asynchronous so it doesnt add unnecessary latency to the UI.
@ -68,7 +85,7 @@
*
* @param key The unique key used to store the wanted image
*/
- (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image))doneBlock;
- (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock;
/**
* Remove the image from memory and disk cache synchronousely

View File

@ -162,13 +162,13 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
[self storeImage:image imageData:nil forKey:key toDisk:toDisk];
}
- (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image))doneBlock
- (void)queryDiskCacheForKey:(NSString *)key done:(void (^)(UIImage *image, SDImageCacheType cacheType))doneBlock
{
if (!doneBlock) return;
if (!key)
{
doneBlock(nil);
doneBlock(nil, SDImageCacheTypeNone);
return;
}
@ -176,7 +176,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
UIImage *image = [self.memCache objectForKey:key];
if (image)
{
doneBlock(image);
doneBlock(image, SDImageCacheTypeMemory);
return;
}
@ -189,7 +189,7 @@ static const NSInteger kDefaultCacheMaxCacheAge = 60 * 60 * 24 * 7; // 1 week
[self.memCache setObject:diskImage forKey:key cost:image.size.height * image.size.width * image.scale];
}
doneBlock(diskImage);
doneBlock(diskImage, SDImageCacheTypeDisk);
});
}

View File

@ -34,8 +34,8 @@ typedef enum
SDWebImageProgressiveDownload = 1 << 3
} SDWebImageOptions;
typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, BOOL fromCache);
typedef void(^SDWebImageCompletedWithFinishedBlock)(UIImage *image, NSError *error, BOOL fromCache, BOOL finished);
typedef void(^SDWebImageCompletedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType);
typedef void(^SDWebImageCompletedWithFinishedBlock)(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished);
/**

View File

@ -73,14 +73,14 @@
if (!url || !completedBlock || (!(options & SDWebImageRetryFailed) && [self.failedURLs containsObject:url]))
{
if (completedBlock) completedBlock(nil, nil, NO, NO);
if (completedBlock) completedBlock(nil, nil, SDImageCacheTypeNone, NO);
return operation;
}
[self.runningOperations addObject:operation];
NSString *key = [self cacheKeyForURL:url];
[self.imageCache queryDiskCacheForKey:key done:^(UIImage *image)
[self.imageCache queryDiskCacheForKey:key done:^(UIImage *image, SDImageCacheType cacheType)
{
if (operation.isCancelled) return;
@ -88,7 +88,7 @@
{
dispatch_async(dispatch_get_main_queue(), ^
{
completedBlock(image, nil, YES, YES);
completedBlock(image, nil, cacheType, YES);
[self.runningOperations removeObject:operation];
});
}
@ -101,7 +101,7 @@
{
dispatch_async(dispatch_get_main_queue(), ^
{
completedBlock(downloadedImage, error, NO, finished);
completedBlock(downloadedImage, error, SDImageCacheTypeNone, finished);
if (error)
{

View File

@ -46,7 +46,7 @@
{
if (index >= self.prefetchURLs.count) return;
self.requestedCount++;
[self.manager downloadWithURL:self.prefetchURLs[index] options:self.options progress:nil completed:^(UIImage *image, NSError *error, BOOL fromCache, BOOL finished)
[self.manager downloadWithURL:self.prefetchURLs[index] options:self.options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
if (!finished) return;
self.finishedCount++;

View File

@ -45,7 +45,7 @@ static char operationKey;
if (url)
{
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, BOOL fromCache, BOOL finished)
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
if (image)
{
@ -53,7 +53,7 @@ static char operationKey;
}
if (completedBlock && finished)
{
completedBlock(image, error, fromCache);
completedBlock(image, error, cacheType);
}
}];
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
@ -93,7 +93,7 @@ static char operationKey;
if (url)
{
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, BOOL fromCache, BOOL finished)
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
if (image)
{
@ -101,7 +101,7 @@ static char operationKey;
}
if (completedBlock && finished)
{
completedBlock(image, error, fromCache);
completedBlock(image, error, cacheType);
}
}];
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);

View File

@ -51,7 +51,7 @@ static char operationKey;
if (url)
{
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, BOOL fromCache, BOOL finished)
id<SDWebImageOperation> operation = [SDWebImageManager.sharedManager downloadWithURL:url options:options progress:progressBlock completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished)
{
if (image)
{
@ -60,7 +60,7 @@ static char operationKey;
}
if (completedBlock && finished)
{
completedBlock(image, error, fromCache);
completedBlock(image, error, cacheType);
}
}];
objc_setAssociatedObject(self, &operationKey, operation, OBJC_ASSOCIATION_RETAIN_NONATOMIC);