From 80dace4a4a46d579efe79b0230987cccfba43ecc Mon Sep 17 00:00:00 2001 From: iwill Date: Fri, 18 Aug 2017 12:02:06 +0800 Subject: [PATCH 1/2] Fix issue #2001, add sd_currentBackgroundImageURL and sd_backgroundImageURLForState: for UIButton --- SDWebImage/UIButton+WebCache.h | 16 ++++++++++++++-- SDWebImage/UIButton+WebCache.m | 26 ++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 6 deletions(-) diff --git a/SDWebImage/UIButton+WebCache.h b/SDWebImage/UIButton+WebCache.h index f47ab8c8..b6c4a2a2 100644 --- a/SDWebImage/UIButton+WebCache.h +++ b/SDWebImage/UIButton+WebCache.h @@ -17,13 +17,13 @@ */ @interface UIButton (WebCache) +#pragma mark - Image + /** * Get the current image URL. */ - (nullable NSURL *)sd_currentImageURL; -#pragma mark - Image - /** * Get the image URL for a control state. * @@ -130,6 +130,18 @@ #pragma mark - Background image +/** + * Get the current background image URL. + */ +- (nullable NSURL *)sd_currentBackgroundImageURL; + +/** + * Get the background image URL for a control state. + * + * @param state Which state you want to know the URL for. The values are described in UIControlState. + */ +- (nullable NSURL *)sd_backgroundImageURLForState:(UIControlState)state; + /** * Set the backgroundImageView `image` with an `url`. * diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index 63f75e40..ce9a52e6 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -18,8 +18,14 @@ static char imageURLStorageKey; typedef NSMutableDictionary SDStateImageURLDictionary; +static inline NSNumber * backgroundImageURLKeyForState(UIControlState state) { + return @(NSUIntegerMax - state - 1); +} + @implementation UIButton (WebCache) +#pragma mark - Image + - (nullable NSURL *)sd_currentImageURL { NSURL *url = self.imageURLStorage[@(self.state)]; @@ -34,8 +40,6 @@ typedef NSMutableDictionary SDStateImageURLDictionary; return self.imageURLStorage[@(state)]; } -#pragma mark - Image - - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; } @@ -82,6 +86,20 @@ typedef NSMutableDictionary SDStateImageURLDictionary; #pragma mark - Background image +- (nullable NSURL *)sd_currentBackgroundImageURL { + NSURL *url = self.imageURLStorage[backgroundImageURLKeyForState(self.state)]; + + if (!url) { + url = self.imageURLStorage[backgroundImageURLKeyForState(UIControlStateNormal)]; + } + + return url; +} + +- (nullable NSURL *)sd_backgroundImageURLForState:(UIControlState)state { + return self.imageURLStorage[backgroundImageURLKeyForState(state)]; +} + - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; } @@ -108,11 +126,11 @@ typedef NSMutableDictionary SDStateImageURLDictionary; options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock { if (!url) { - [self.imageURLStorage removeObjectForKey:@(state)]; + [self.imageURLStorage removeObjectForKey:backgroundImageURLKeyForState(state)]; return; } - self.imageURLStorage[@(state)] = url; + self.imageURLStorage[backgroundImageURLKeyForState(state)] = url; __weak typeof(self)weakSelf = self; [self sd_internalSetImageWithURL:url From de897ae33a79f72fad12a75f22fb43ab8db580c6 Mon Sep 17 00:00:00 2001 From: iwill Date: Fri, 18 Aug 2017 13:55:34 +0800 Subject: [PATCH 2/2] Fix issue #2001 --- SDWebImage/UIButton+WebCache.m | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index ce9a52e6..c678ed0b 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -16,10 +16,14 @@ static char imageURLStorageKey; -typedef NSMutableDictionary SDStateImageURLDictionary; +typedef NSMutableDictionary SDStateImageURLDictionary; -static inline NSNumber * backgroundImageURLKeyForState(UIControlState state) { - return @(NSUIntegerMax - state - 1); +static inline NSString * imageURLKeyForState(UIControlState state) { + return [NSString stringWithFormat:@"image_%lu", (unsigned long)state]; +} + +static inline NSString * backgroundImageURLKeyForState(UIControlState state) { + return [NSString stringWithFormat:@"backgroundImage_%lu", (unsigned long)state]; } @implementation UIButton (WebCache) @@ -27,17 +31,17 @@ static inline NSNumber * backgroundImageURLKeyForState(UIControlState state) { #pragma mark - Image - (nullable NSURL *)sd_currentImageURL { - NSURL *url = self.imageURLStorage[@(self.state)]; + NSURL *url = self.imageURLStorage[imageURLKeyForState(self.state)]; if (!url) { - url = self.imageURLStorage[@(UIControlStateNormal)]; + url = self.imageURLStorage[imageURLKeyForState(UIControlStateNormal)]; } return url; } - (nullable NSURL *)sd_imageURLForState:(UIControlState)state { - return self.imageURLStorage[@(state)]; + return self.imageURLStorage[imageURLKeyForState(state)]; } - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { @@ -66,11 +70,11 @@ static inline NSNumber * backgroundImageURLKeyForState(UIControlState state) { options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock { if (!url) { - [self.imageURLStorage removeObjectForKey:@(state)]; + [self.imageURLStorage removeObjectForKey:imageURLKeyForState(state)]; return; } - self.imageURLStorage[@(state)] = url; + self.imageURLStorage[imageURLKeyForState(state)] = url; __weak typeof(self)weakSelf = self; [self sd_internalSetImageWithURL:url