Authored by iwill

Fix issue #2001, add sd_currentBackgroundImageURL and sd_backgroundImageURLForState: for UIButton

@@ -17,13 +17,13 @@ @@ -17,13 +17,13 @@
17 */ 17 */
18 @interface UIButton (WebCache) 18 @interface UIButton (WebCache)
19 19
  20 +#pragma mark - Image
  21 +
20 /** 22 /**
21 * Get the current image URL. 23 * Get the current image URL.
22 */ 24 */
23 - (nullable NSURL *)sd_currentImageURL; 25 - (nullable NSURL *)sd_currentImageURL;
24 26
25 -#pragma mark - Image  
26 -  
27 /** 27 /**
28 * Get the image URL for a control state. 28 * Get the image URL for a control state.
29 * 29 *
@@ -131,6 +131,18 @@ @@ -131,6 +131,18 @@
131 #pragma mark - Background image 131 #pragma mark - Background image
132 132
133 /** 133 /**
  134 + * Get the current background image URL.
  135 + */
  136 +- (nullable NSURL *)sd_currentBackgroundImageURL;
  137 +
  138 +/**
  139 + * Get the background image URL for a control state.
  140 + *
  141 + * @param state Which state you want to know the URL for. The values are described in UIControlState.
  142 + */
  143 +- (nullable NSURL *)sd_backgroundImageURLForState:(UIControlState)state;
  144 +
  145 +/**
134 * Set the backgroundImageView `image` with an `url`. 146 * Set the backgroundImageView `image` with an `url`.
135 * 147 *
136 * The download is asynchronous and cached. 148 * The download is asynchronous and cached.
@@ -18,8 +18,14 @@ static char imageURLStorageKey; @@ -18,8 +18,14 @@ static char imageURLStorageKey;
18 18
19 typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; 19 typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary;
20 20
  21 +static inline NSNumber * backgroundImageURLKeyForState(UIControlState state) {
  22 + return @(NSUIntegerMax - state - 1);
  23 +}
  24 +
21 @implementation UIButton (WebCache) 25 @implementation UIButton (WebCache)
22 26
  27 +#pragma mark - Image
  28 +
23 - (nullable NSURL *)sd_currentImageURL { 29 - (nullable NSURL *)sd_currentImageURL {
24 NSURL *url = self.imageURLStorage[@(self.state)]; 30 NSURL *url = self.imageURLStorage[@(self.state)];
25 31
@@ -34,8 +40,6 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; @@ -34,8 +40,6 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary;
34 return self.imageURLStorage[@(state)]; 40 return self.imageURLStorage[@(state)];
35 } 41 }
36 42
37 -#pragma mark - Image  
38 -  
39 - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { 43 - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {
40 [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; 44 [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
41 } 45 }
@@ -82,6 +86,20 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; @@ -82,6 +86,20 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary;
82 86
83 #pragma mark - Background image 87 #pragma mark - Background image
84 88
  89 +- (nullable NSURL *)sd_currentBackgroundImageURL {
  90 + NSURL *url = self.imageURLStorage[backgroundImageURLKeyForState(self.state)];
  91 +
  92 + if (!url) {
  93 + url = self.imageURLStorage[backgroundImageURLKeyForState(UIControlStateNormal)];
  94 + }
  95 +
  96 + return url;
  97 +}
  98 +
  99 +- (nullable NSURL *)sd_backgroundImageURLForState:(UIControlState)state {
  100 + return self.imageURLStorage[backgroundImageURLKeyForState(state)];
  101 +}
  102 +
85 - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { 103 - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {
86 [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; 104 [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
87 } 105 }
@@ -108,11 +126,11 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; @@ -108,11 +126,11 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary;
108 options:(SDWebImageOptions)options 126 options:(SDWebImageOptions)options
109 completed:(nullable SDExternalCompletionBlock)completedBlock { 127 completed:(nullable SDExternalCompletionBlock)completedBlock {
110 if (!url) { 128 if (!url) {
111 - [self.imageURLStorage removeObjectForKey:@(state)]; 129 + [self.imageURLStorage removeObjectForKey:backgroundImageURLKeyForState(state)];
112 return; 130 return;
113 } 131 }
114 132
115 - self.imageURLStorage[@(state)] = url; 133 + self.imageURLStorage[backgroundImageURLKeyForState(state)] = url;
116 134
117 __weak typeof(self)weakSelf = self; 135 __weak typeof(self)weakSelf = self;
118 [self sd_internalSetImageWithURL:url 136 [self sd_internalSetImageWithURL:url