From b734f289d03ab5b66ebae7616166949adea34456 Mon Sep 17 00:00:00 2001 From: Olivier Poitrey Date: Fri, 7 Sep 2012 22:58:18 +0200 Subject: [PATCH] Add a cached parameter to the success block to tell the receiver if the image came from cache or network #181 --- README.md | 4 ++-- SDWebImage/MKAnnotationView+WebCache.h | 12 ++++++------ SDWebImage/MKAnnotationView+WebCache.m | 6 +++--- SDWebImage/SDWebImageManager.h | 11 ++++++++--- SDWebImage/SDWebImageManager.m | 25 ++++++++++--------------- SDWebImage/UIButton+WebCache.h | 12 ++++++------ SDWebImage/UIButton+WebCache.m | 12 ++++++------ SDWebImage/UIImageView+WebCache.h | 6 +++--- SDWebImage/UIImageView+WebCache.m | 6 +++--- 9 files changed, 47 insertions(+), 47 deletions(-) diff --git a/README.md b/README.md index 2b119e23..57f4ea92 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ notified when image have been retrieved from cache. // 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"] - success:^(UIImage *image) {... success code here ...} + success:^(UIImage *image, BOOL cached) {... success code here ...} failure:^(NSError *error) {... failure code here ...}]; ]; ``` @@ -120,7 +120,7 @@ SDWebImageManager *manager = [SDWebImageManager sharedManager]; [manager downloadWithURL:imageURL delegate:self options:0 - success:^(UIImage *image) + success:^(UIImage *image, BOOL cached) { // do something with image } diff --git a/SDWebImage/MKAnnotationView+WebCache.h b/SDWebImage/MKAnnotationView+WebCache.h index 4376f4de..2de424d5 100644 --- a/SDWebImage/MKAnnotationView+WebCache.h +++ b/SDWebImage/MKAnnotationView+WebCache.h @@ -54,10 +54,10 @@ * The downloand is asynchronous and cached. * * @param url The url for the image. - * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. + * @param success A block to be executed when the image request succeed This block has no return value and takes a Boolean as parameter indicating if the image was cached or not. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; /** * Set the imageView `image` with an `url`, placeholder. @@ -66,10 +66,10 @@ * * @param url The url for the image. * @param placeholder The image to be set initially, until the image request finishes. - * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. + * @param success A block to be executed when the image request succeed This block has no return value and takes a Boolean as parameter indicating if the image was cached or not. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; /** * Set the imageView `image` with an `url`, placeholder and custom options. @@ -79,10 +79,10 @@ * @param url The url for the image. * @param placeholder The image to be set initially, until the image request finishes. * @param options The options to use when downloading the image. @see SDWebImageOptions for the possible values. - * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. + * @param success A block to be executed when the image request succeed This block has no return value and takes a Boolean as parameter indicating if the image was cached or not. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; #endif /** diff --git a/SDWebImage/MKAnnotationView+WebCache.m b/SDWebImage/MKAnnotationView+WebCache.m index 3e6d2e76..0fe94570 100644 --- a/SDWebImage/MKAnnotationView+WebCache.m +++ b/SDWebImage/MKAnnotationView+WebCache.m @@ -36,17 +36,17 @@ } #if NS_BLOCKS_AVAILABLE -- (void)setImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { [self setImageWithURL:url placeholderImage:nil success:success failure:failure]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { [self setImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { SDWebImageManager *manager = [SDWebImageManager sharedManager]; diff --git a/SDWebImage/SDWebImageManager.h b/SDWebImage/SDWebImageManager.h index 90753401..f5287204 100644 --- a/SDWebImage/SDWebImageManager.h +++ b/SDWebImage/SDWebImageManager.h @@ -19,6 +19,11 @@ typedef enum SDWebImageProgressiveDownload = 1 << 3 } SDWebImageOptions; +#if NS_BLOCKS_AVAILABLE +typedef void(^SDWebImageSuccessBlock)(UIImage *image, BOOL cached); +typedef void(^SDWebImageFailureBlock)(NSError *error); +#endif + /** * The SDWebImageManager is the class behind the UIImageView+WebCache category and likes. * It ties the asynchronous downloader (SDWebImageDownloader) with the image cache store (SDImageCache). @@ -31,7 +36,7 @@ typedef enum * [manager downloadWithURL:imageURL * delegate:self * options:0 - * success:^(UIImage *image) + * success:^(UIImage *image, BOOL cached) * { * // do something with image * } @@ -125,7 +130,7 @@ typedef NSString *(^CacheKeyFilter)(NSURL *url); * @param failure A block called when couldn't be retrived for some reason * @see [SDWebImageManager downloadWithURL:delegate:options:] */ -- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; /** * Downloads the image at the given URL if not present in cache or return the cached version otherwise. @@ -138,7 +143,7 @@ typedef NSString *(^CacheKeyFilter)(NSURL *url); * @param failure A block called when couldn't be retrived for some reason * @see [SDWebImageManager downloadWithURL:delegate:options:] */ -- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options userInfo:(NSDictionary *)info success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options userInfo:(NSDictionary *)info success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; #endif /** diff --git a/SDWebImage/SDWebImageManager.m b/SDWebImage/SDWebImageManager.m index 08226adb..22e49b6b 100644 --- a/SDWebImage/SDWebImageManager.m +++ b/SDWebImage/SDWebImageManager.m @@ -11,11 +11,6 @@ #import "SDWebImageDownloader.h" #import -#if NS_BLOCKS_AVAILABLE -typedef void(^SuccessBlock)(UIImage *image); -typedef void(^FailureBlock)(NSError *error); -#endif - static SDWebImageManager *instance; @implementation SDWebImageManager @@ -146,12 +141,12 @@ static SDWebImageManager *instance; } #if NS_BLOCKS_AVAILABLE -- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure +- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure { [self downloadWithURL:url delegate:delegate options:options userInfo:nil success:success failure:failure]; } -- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options userInfo:(NSDictionary *)userInfo success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure +- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options userInfo:(NSDictionary *)userInfo success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure { // repeated logic from above due to requirement for backwards compatability for iOS versions without blocks @@ -170,8 +165,8 @@ static SDWebImageManager *instance; // Check the on-disk cache async so we don't block the main thread [cacheDelegates addObject:delegate]; [cacheURLs addObject:url]; - SuccessBlock successCopy = [success copy]; - FailureBlock failureCopy = [failure copy]; + SDWebImageSuccessBlock successCopy = [success copy]; + SDWebImageFailureBlock failureCopy = [failure copy]; NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys: delegate, @"delegate", url, @"url", @@ -262,8 +257,8 @@ static SDWebImageManager *instance; #if NS_BLOCKS_AVAILABLE if ([info objectForKey:@"success"]) { - SuccessBlock success = [info objectForKey:@"success"]; - success(image); + SDWebImageSuccessBlock success = [info objectForKey:@"success"]; + success(image, YES); } #endif @@ -382,8 +377,8 @@ static SDWebImageManager *instance; #if NS_BLOCKS_AVAILABLE if ([[downloadInfo objectAtIndex:uidx] objectForKey:@"success"]) { - SuccessBlock success = [[downloadInfo objectAtIndex:uidx] objectForKey:@"success"]; - success(image); + SDWebImageSuccessBlock success = [[downloadInfo objectAtIndex:uidx] objectForKey:@"success"]; + success(image, NO); } #endif } @@ -409,7 +404,7 @@ static SDWebImageManager *instance; #if NS_BLOCKS_AVAILABLE if ([[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"]) { - FailureBlock failure = [[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"]; + SDWebImageFailureBlock failure = [[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"]; failure(nil); } #endif @@ -477,7 +472,7 @@ static SDWebImageManager *instance; #if NS_BLOCKS_AVAILABLE if ([[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"]) { - FailureBlock failure = [[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"]; + SDWebImageFailureBlock failure = [[downloadInfo objectAtIndex:uidx] objectForKey:@"failure"]; failure(error); } #endif diff --git a/SDWebImage/UIButton+WebCache.h b/SDWebImage/UIButton+WebCache.h index c8ad321a..26ee9275 100644 --- a/SDWebImage/UIButton+WebCache.h +++ b/SDWebImage/UIButton+WebCache.h @@ -56,7 +56,7 @@ * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; /** * Set the imageView `image` with an `url`, placeholder. @@ -68,7 +68,7 @@ * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; /** * Set the imageView `image` with an `url`, placeholder and custom options. @@ -81,7 +81,7 @@ * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; #endif /** @@ -125,7 +125,7 @@ * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setBackgroundImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setBackgroundImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; /** * Set the backgroundImageView `image` with an `url`, placeholder. @@ -137,7 +137,7 @@ * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; /** * Set the backgroundImageView `image` with an `url`, placeholder and custom options. @@ -150,7 +150,7 @@ * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; #endif diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index 5c97ee39..2b61c1a8 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -40,17 +40,17 @@ } #if NS_BLOCKS_AVAILABLE -- (void)setImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { [self setImageWithURL:url placeholderImage:nil success:success failure:failure]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { [self setImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { SDWebImageManager *manager = [SDWebImageManager sharedManager]; @@ -97,17 +97,17 @@ } #if NS_BLOCKS_AVAILABLE -- (void)setBackgroundImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setBackgroundImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { [self setBackgroundImageWithURL:url placeholderImage:nil success:success failure:failure]; } -- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { [self setBackgroundImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure]; } -- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { SDWebImageManager *manager = [SDWebImageManager sharedManager]; diff --git a/SDWebImage/UIImageView+WebCache.h b/SDWebImage/UIImageView+WebCache.h index e7f4c079..7a83c200 100644 --- a/SDWebImage/UIImageView+WebCache.h +++ b/SDWebImage/UIImageView+WebCache.h @@ -85,7 +85,7 @@ * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; /** * Set the imageView `image` with an `url`, placeholder. @@ -97,7 +97,7 @@ * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; /** * Set the imageView `image` with an `url`, placeholder and custom options. @@ -110,7 +110,7 @@ * @param success A block to be executed when the image request succeed This block has no return value and takes the retrieved image as argument. * @param failure A block object to be executed when the image request failed. This block has no return value and takes the error object describing the network or parsing error that occurred (may be nil). */ -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; #endif /** diff --git a/SDWebImage/UIImageView+WebCache.m b/SDWebImage/UIImageView+WebCache.m index 52bcdc4a..090c82cd 100644 --- a/SDWebImage/UIImageView+WebCache.m +++ b/SDWebImage/UIImageView+WebCache.m @@ -36,17 +36,17 @@ } #if NS_BLOCKS_AVAILABLE -- (void)setImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { [self setImageWithURL:url placeholderImage:nil success:success failure:failure]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { [self setImageWithURL:url placeholderImage:placeholder options:0 success:success failure:failure]; } -- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure; +- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(SDWebImageSuccessBlock)success failure:(SDWebImageFailureBlock)failure; { SDWebImageManager *manager = [SDWebImageManager sharedManager];