Skip to content

Commit

Permalink
Add support for setBackgroundImage:* in UIButton category (fix SDWebI…
Browse files Browse the repository at this point in the history
  • Loading branch information
Olivier Poitrey committed May 14, 2012
1 parent eff6cac commit 874f944
Show file tree
Hide file tree
Showing 5 changed files with 264 additions and 14 deletions.
32 changes: 29 additions & 3 deletions SDWebImage/SDWebImageManager.h
Expand Up @@ -81,8 +81,8 @@ typedef NSString *(^CacheKeyFilter)(NSURL *url);
*
* @param url The URL to the image
* @param delegate The delegate object used to send result back
* @see [SDWebImageManager downloadWithURL:delegate:options:]
* @see [SDWebImageManager downloadWithURL:delegate:options:success:failure:]
* @see [SDWebImageManager downloadWithURL:delegate:options:userInfo:]
* @see [SDWebImageManager downloadWithURL:delegate:options:userInfo:success:failure:]
*/
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate;

Expand All @@ -92,9 +92,22 @@ typedef NSString *(^CacheKeyFilter)(NSURL *url);
* @param url The URL to the image
* @param delegate The delegate object used to send result back
* @param options A mask to specify options to use for this request
* @see [SDWebImageManager downloadWithURL:delegate:options:success:failure:]
* @see [SDWebImageManager downloadWithURL:delegate:options:userInfo:]
* @see [SDWebImageManager downloadWithURL:delegate:options:userInfo:success:failure:]
*/
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate options:(SDWebImageOptions)options;

/**
* Downloads the image at the given URL if not present in cache or return the cached version otherwise.
*
* @param url The URL to the image
* @param delegate The delegate object used to send result back
* @param options A mask to specify options to use for this request
* @param info An NSDictionnary passed back to delegate if provided
* @see [SDWebImageManager downloadWithURL:delegate:options:success:failure:]
*/
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate options:(SDWebImageOptions)options userInfo:(NSDictionary *)info;

// use options:SDWebImageRetryFailed instead
- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate retryFailed:(BOOL)retryFailed __attribute__ ((deprecated));
// use options:SDWebImageRetryFailed|SDWebImageLowPriority instead
Expand All @@ -112,6 +125,19 @@ typedef NSString *(^CacheKeyFilter)(NSURL *url);
* @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;

/**
* Downloads the image at the given URL if not present in cache or return the cached version otherwise.
*
* @param url The URL to the image
* @param delegate The delegate object used to send result back
* @param options A mask to specify options to use for this request
* @param info An NSDictionnary passed back to delegate if provided
* @param success A block called when image has been retrived successfuly
* @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;
#endif

/**
Expand Down
73 changes: 70 additions & 3 deletions SDWebImage/SDWebImageManager.m
Expand Up @@ -109,6 +109,11 @@ - (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)del
}

- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate options:(SDWebImageOptions)options
{
[self downloadWithURL:url delegate:delegate options:options userInfo:nil];
}

- (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)delegate options:(SDWebImageOptions)options userInfo:(NSDictionary *)userInfo
{
// Very common mistake is to send the URL using NSString object instead of NSURL. For some strange reason, XCode won't
// throw any warning for this type mismatch. Here we failsafe this error by allowing URLs to be passed as NSString.
Expand All @@ -125,12 +130,22 @@ - (void)downloadWithURL:(NSURL *)url delegate:(id<SDWebImageManagerDelegate>)del
// Check the on-disk cache async so we don't block the main thread
[cacheDelegates addObject:delegate];
[cacheURLs addObject:url];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:delegate, @"delegate", url, @"url", [NSNumber numberWithInt:options], @"options", nil];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
delegate, @"delegate",
url, @"url",
[NSNumber numberWithInt:options], @"options",
userInfo ? userInfo : [NSNull null], @"userInfo",
nil];
[[SDImageCache sharedImageCache] queryDiskCacheForKey:[self cacheKeyForURL:url] delegate:self userInfo:info];
}

#if NS_BLOCKS_AVAILABLE
- (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))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
{
// repeated logic from above due to requirement for backwards compatability for iOS versions without blocks

Expand All @@ -151,7 +166,14 @@ - (void)downloadWithURL:(NSURL *)url delegate:(id)delegate options:(SDWebImageOp
[cacheURLs addObject:url];
SuccessBlock successCopy = [success copy];
FailureBlock failureCopy = [failure copy];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:delegate, @"delegate", url, @"url", [NSNumber numberWithInt:options], @"options", successCopy, @"success", failureCopy, @"failure", nil];
NSDictionary *info = [NSDictionary dictionaryWithObjectsAndKeys:
delegate, @"delegate",
url, @"url",
[NSNumber numberWithInt:options], @"options",
userInfo ? userInfo : [NSNull null], @"userInfo",
successCopy, @"success",
failureCopy, @"failure",
nil];
SDWIRelease(successCopy);
SDWIRelease(failureCopy);
[[SDImageCache sharedImageCache] queryDiskCacheForKey:[self cacheKeyForURL:url] delegate:self userInfo:info];
Expand Down Expand Up @@ -221,6 +243,15 @@ - (void)imageCache:(SDImageCache *)imageCache didFindImage:(UIImage *)image forK
{
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:), self, image, url);
}
if ([delegate respondsToSelector:@selector(webImageManager:didFinishWithImage:forURL:userInfo:)])
{
NSDictionary *userInfo = [info objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:userInfo:), self, image, url, userInfo);
}
#if NS_BLOCKS_AVAILABLE
if ([info objectForKey:@"success"])
{
Expand Down Expand Up @@ -260,7 +291,7 @@ - (void)imageCache:(SDImageCache *)imageCache didNotFindImageForKey:(NSString *)
else
{
// Reuse shared downloader
downloader.userInfo = info;
downloader.userInfo = info; // TOFIX: here we overload previous userInfo
downloader.lowPriority = (options & SDWebImageLowPriority);
}

Expand Down Expand Up @@ -293,6 +324,15 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didUpdatePartialImage
{
objc_msgSend(delegate, @selector(webImageManager:didProgressWithPartialImage:forURL:), self, image, downloader.url);
}
if ([delegate respondsToSelector:@selector(webImageManager:didProgressWithPartialImage:forURL:userInfo:)])
{
NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didProgressWithPartialImage:forURL:userInfo:), self, image, downloader.url, userInfo);
}
}
}
}
Expand Down Expand Up @@ -323,6 +363,15 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
{
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:), self, image, downloader.url);
}
if ([delegate respondsToSelector:@selector(webImageManager:didFinishWithImage:forURL:userInfo:)])
{
NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFinishWithImage:forURL:userInfo:), self, image, downloader.url, userInfo);
}
#if NS_BLOCKS_AVAILABLE
if ([downloader.userInfo objectForKey:@"success"])
{
Expand All @@ -341,6 +390,15 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFinishWithImage:(U
{
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, nil, downloader.url);
}
if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:userInfo:)])
{
NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, nil, downloader.url, userInfo);
}
#if NS_BLOCKS_AVAILABLE
if ([downloader.userInfo objectForKey:@"failure"])
{
Expand Down Expand Up @@ -399,6 +457,15 @@ - (void)imageDownloader:(SDWebImageDownloader *)downloader didFailWithError:(NSE
{
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:), self, error, downloader.url);
}
if ([delegate respondsToSelector:@selector(webImageManager:didFailWithError:forURL:userInfo:)])
{
NSDictionary *userInfo = [downloader.userInfo objectForKey:@"userInfo"];
if ([userInfo isKindOfClass:NSNull.class])
{
userInfo = nil;
}
objc_msgSend(delegate, @selector(webImageManager:didFailWithError:forURL:userInfo:), self, error, downloader.url, userInfo);
}
#if NS_BLOCKS_AVAILABLE
if ([downloader.userInfo objectForKey:@"failure"])
{
Expand Down
10 changes: 10 additions & 0 deletions SDWebImage/SDWebImageManagerDelegate.h
Expand Up @@ -19,7 +19,13 @@
/**
* Called while an image is downloading with an partial image object representing the currently downloaded portion of the image.
* This delegate is called only if ImageIO is available and `SDWebImageProgressiveDownload` option has been used.
*
* @param imageManager The image manager
* @param image The retrived image object
* @param url The image URL used to retrive the image
* @param info The user info dictionnary
*/
- (void)webImageManager:(SDWebImageManager *)imageManager didProgressWithPartialImage:(UIImage *)image forURL:(NSURL *)url userInfo:(NSDictionary *)info;
- (void)webImageManager:(SDWebImageManager *)imageManager didProgressWithPartialImage:(UIImage *)image forURL:(NSURL *)url;

/**
Expand All @@ -28,7 +34,9 @@
* @param imageManager The image manager
* @param image The retrived image object
* @param url The image URL used to retrive the image
* @param info The user info dictionnary
*/
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image forURL:(NSURL *)url userInfo:(NSDictionary *)info;
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image forURL:(NSURL *)url;
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image;

Expand All @@ -38,7 +46,9 @@
* @param imageManager The image manager
* @param error The error
* @param url The image URL used to retrive the image
* @param info The user info dictionnary
*/
- (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error forURL:(NSURL *)url userInfo:(NSDictionary *)info;
- (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error forURL:(NSURL *)url;
- (void)webImageManager:(SDWebImageManager *)imageManager didFailWithError:(NSError *)error;

Expand Down
70 changes: 70 additions & 0 deletions SDWebImage/UIButton+WebCache.h
Expand Up @@ -84,6 +84,76 @@
- (void)setImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))failure;
#endif

/**
* Set the backgroundImageView `image` with an `url`.
*
* The downloand is asynchronous and cached.
*
* @param url The url for the image.
*/
- (void)setBackgroundImageWithURL:(NSURL *)url;

/**
* Set the backgroundImageView `image` with an `url` and a placeholder.
*
* The downloand is asynchronous and cached.
*
* @param url The url for the image.
* @param placeholder The image to be set initially, until the image request finishes.
* @see setImageWithURL:placeholderImage:options:
*/
- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder;

/**
* Set the backgroundImageView `image` with an `url`, placeholder and custom options.
*
* The downloand is asynchronous and cached.
*
* @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.
*/
- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options;

#if NS_BLOCKS_AVAILABLE
/**
* Set the backgroundImageView `image` with an `url`.
*
* 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 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;

/**
* Set the backgroundImageView `image` with an `url`, placeholder.
*
* The downloand is asynchronous and cached.
*
* @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 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;

/**
* Set the backgroundImageView `image` with an `url`, placeholder and custom options.
*
* The downloand is asynchronous and cached.
*
* @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 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;
#endif


/**
* Cancel the current download
*/
Expand Down

0 comments on commit 874f944

Please sign in to comment.