Skip to content

Commit

Permalink
Add a cached parameter to the success block to tell the receiver if t…
Browse files Browse the repository at this point in the history
…he image came from cache or network #181
  • Loading branch information
Olivier Poitrey committed Sep 7, 2012
1 parent 3cdbb9b commit b734f28
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 47 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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 ...}];
];
```
Expand All @@ -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
}
Expand Down
12 changes: 6 additions & 6 deletions SDWebImage/MKAnnotationView+WebCache.h
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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

/**
Expand Down
6 changes: 3 additions & 3 deletions SDWebImage/MKAnnotationView+WebCache.m
Expand Up @@ -36,17 +36,17 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt
}

#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];

Expand Down
11 changes: 8 additions & 3 deletions SDWebImage/SDWebImageManager.h
Expand Up @@ -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).
Expand All @@ -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
* }
Expand Down Expand Up @@ -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.
Expand All @@ -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

/**
Expand Down
25 changes: 10 additions & 15 deletions SDWebImage/SDWebImageManager.m
Expand Up @@ -11,11 +11,6 @@
#import "SDWebImageDownloader.h"
#import <objc/message.h>

#if NS_BLOCKS_AVAILABLE
typedef void(^SuccessBlock)(UIImage *image);
typedef void(^FailureBlock)(NSError *error);
#endif

static SDWebImageManager *instance;

@implementation SDWebImageManager
Expand Down Expand Up @@ -146,12 +141,12 @@ - (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)del
}

#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

Expand All @@ -170,8 +165,8 @@ - (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOp
// 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",
Expand Down Expand Up @@ -262,8 +257,8 @@ - (void)imageCache:(SDImageCache *)imageCache didFindImage:(UIImage *)image forK
#if NS_BLOCKS_AVAILABLE
if ([info objectForKey:@"success"])
{
SuccessBlock success = [info objectForKey:@"success"];
success(image);
SDWebImageSuccessBlock success = [info objectForKey:@"success"];
success(image, YES);
}
#endif

Expand Down Expand Up @@ -382,8 +377,8 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
#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
}
Expand All @@ -409,7 +404,7 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
#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
Expand Down Expand Up @@ -477,7 +472,7 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFailWithError:(NSE
#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
Expand Down
12 changes: 6 additions & 6 deletions SDWebImage/UIButton+WebCache.h
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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

/**
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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


Expand Down
12 changes: 6 additions & 6 deletions SDWebImage/UIButton+WebCache.m
Expand Up @@ -40,17 +40,17 @@ - (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder opt
}

#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];

Expand Down Expand Up @@ -97,17 +97,17 @@ - (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)place
}

#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];

Expand Down

0 comments on commit b734f28

Please sign in to comment.