Add support for setBackgroundImage:* in UIButton category (fix #121)
This commit is contained in:
parent
eff6cac284
commit
874f944d8e
|
@ -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;
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
||||
/**
|
||||
|
|
|
@ -109,6 +109,11 @@ static SDWebImageManager *instance;
|
|||
}
|
||||
|
||||
- (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.
|
||||
|
@ -125,12 +130,22 @@ static SDWebImageManager *instance;
|
|||
// 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
|
||||
|
||||
|
@ -151,7 +166,14 @@ static SDWebImageManager *instance;
|
|||
[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];
|
||||
|
@ -221,6 +243,15 @@ static SDWebImageManager *instance;
|
|||
{
|
||||
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"])
|
||||
{
|
||||
|
@ -260,7 +291,7 @@ static SDWebImageManager *instance;
|
|||
else
|
||||
{
|
||||
// Reuse shared downloader
|
||||
downloader.userInfo = info;
|
||||
downloader.userInfo = info; // TOFIX: here we overload previous userInfo
|
||||
downloader.lowPriority = (options & SDWebImageLowPriority);
|
||||
}
|
||||
|
||||
|
@ -293,6 +324,15 @@ static SDWebImageManager *instance;
|
|||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -323,6 +363,15 @@ static SDWebImageManager *instance;
|
|||
{
|
||||
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"])
|
||||
{
|
||||
|
@ -341,6 +390,15 @@ static SDWebImageManager *instance;
|
|||
{
|
||||
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"])
|
||||
{
|
||||
|
@ -399,6 +457,15 @@ static SDWebImageManager *instance;
|
|||
{
|
||||
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"])
|
||||
{
|
||||
|
|
|
@ -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;
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
|
|
@ -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
|
||||
*/
|
||||
|
|
|
@ -68,24 +68,101 @@
|
|||
}
|
||||
#endif
|
||||
|
||||
- (void)setBackgroundImageWithURL:(NSURL *)url
|
||||
{
|
||||
[self setBackgroundImageWithURL:url placeholderImage:nil];
|
||||
}
|
||||
|
||||
- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder
|
||||
{
|
||||
[self setBackgroundImageWithURL:url placeholderImage:placeholder options:0];
|
||||
}
|
||||
|
||||
- (void)setBackgroundImageWithURL:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(SDWebImageOptions)options
|
||||
{
|
||||
SDWebImageManager *manager = [SDWebImageManager sharedManager];
|
||||
|
||||
// Remove in progress downloader from queue
|
||||
[manager cancelForDelegate:self];
|
||||
|
||||
[self setBackgroundImage:placeholder forState:UIControlStateNormal];
|
||||
[self setBackgroundImage:placeholder forState:UIControlStateSelected];
|
||||
[self setBackgroundImage:placeholder forState:UIControlStateHighlighted];
|
||||
|
||||
if (url)
|
||||
{
|
||||
NSDictionary *info = [NSDictionary dictionaryWithObject:@"background" forKey:@"type"];
|
||||
[manager downloadWithURL:url delegate:self options:options userInfo:info];
|
||||
}
|
||||
}
|
||||
|
||||
#if NS_BLOCKS_AVAILABLE
|
||||
- (void)setBackgroundImageWithURL:(NSURL *)url success:(void (^)(UIImage *image))success failure:(void (^)(NSError *error))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;
|
||||
{
|
||||
[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;
|
||||
{
|
||||
SDWebImageManager *manager = [SDWebImageManager sharedManager];
|
||||
|
||||
// Remove in progress downloader from queue
|
||||
[manager cancelForDelegate:self];
|
||||
|
||||
[self setBackgroundImage:placeholder forState:UIControlStateNormal];
|
||||
[self setBackgroundImage:placeholder forState:UIControlStateSelected];
|
||||
[self setBackgroundImage:placeholder forState:UIControlStateHighlighted];
|
||||
|
||||
if (url)
|
||||
{
|
||||
NSDictionary *info = [NSDictionary dictionaryWithObject:@"background" forKey:@"type"];
|
||||
[manager downloadWithURL:url delegate:self options:options userInfo:info success:success failure:failure];
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
- (void)cancelCurrentImageLoad
|
||||
{
|
||||
[[SDWebImageManager sharedManager] cancelForDelegate:self];
|
||||
}
|
||||
|
||||
- (void)webImageManager:(SDWebImageManager *)imageManager didProgressWithPartialImage:(UIImage *)image forURL:(NSURL *)url
|
||||
- (void)webImageManager:(SDWebImageManager *)imageManager didProgressWithPartialImage:(UIImage *)image forURL:(NSURL *)url userInfo:(NSDictionary *)info
|
||||
{
|
||||
[self setImage:image forState:UIControlStateNormal];
|
||||
[self setImage:image forState:UIControlStateSelected];
|
||||
[self setImage:image forState:UIControlStateHighlighted];
|
||||
if ([[info valueForKey:@"type"] isEqualToString:@"background"])
|
||||
{
|
||||
[self setBackgroundImage:image forState:UIControlStateNormal];
|
||||
[self setBackgroundImage:image forState:UIControlStateSelected];
|
||||
[self setBackgroundImage:image forState:UIControlStateHighlighted];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self setImage:image forState:UIControlStateNormal];
|
||||
[self setImage:image forState:UIControlStateSelected];
|
||||
[self setImage:image forState:UIControlStateHighlighted];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image
|
||||
- (void)webImageManager:(SDWebImageManager *)imageManager didFinishWithImage:(UIImage *)image userInfo:(NSDictionary *)info
|
||||
{
|
||||
[self setImage:image forState:UIControlStateNormal];
|
||||
[self setImage:image forState:UIControlStateSelected];
|
||||
[self setImage:image forState:UIControlStateHighlighted];
|
||||
if ([[info valueForKey:@"type"] isEqualToString:@"background"])
|
||||
{
|
||||
[self setBackgroundImage:image forState:UIControlStateNormal];
|
||||
[self setBackgroundImage:image forState:UIControlStateSelected];
|
||||
[self setBackgroundImage:image forState:UIControlStateHighlighted];
|
||||
}
|
||||
else
|
||||
{
|
||||
[self setImage:image forState:UIControlStateNormal];
|
||||
[self setImage:image forState:UIControlStateSelected];
|
||||
[self setImage:image forState:UIControlStateHighlighted];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
|
Loading…
Reference in New Issue