Committed by
GitHub
Merge pull request #2002 from iwill/master
Fix issue #2001, add sd_currentBackgroundImageURL and sd_backgroundIm…
Showing
2 changed files
with
46 additions
and
12 deletions
@@ -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. |
@@ -16,26 +16,34 @@ | @@ -16,26 +16,34 @@ | ||
16 | 16 | ||
17 | static char imageURLStorageKey; | 17 | static char imageURLStorageKey; |
18 | 18 | ||
19 | -typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; | 19 | +typedef NSMutableDictionary<NSString *, NSURL *> SDStateImageURLDictionary; |
20 | + | ||
21 | +static inline NSString * imageURLKeyForState(UIControlState state) { | ||
22 | + return [NSString stringWithFormat:@"image_%lu", (unsigned long)state]; | ||
23 | +} | ||
24 | + | ||
25 | +static inline NSString * backgroundImageURLKeyForState(UIControlState state) { | ||
26 | + return [NSString stringWithFormat:@"backgroundImage_%lu", (unsigned long)state]; | ||
27 | +} | ||
20 | 28 | ||
21 | @implementation UIButton (WebCache) | 29 | @implementation UIButton (WebCache) |
22 | 30 | ||
31 | +#pragma mark - Image | ||
32 | + | ||
23 | - (nullable NSURL *)sd_currentImageURL { | 33 | - (nullable NSURL *)sd_currentImageURL { |
24 | - NSURL *url = self.imageURLStorage[@(self.state)]; | 34 | + NSURL *url = self.imageURLStorage[imageURLKeyForState(self.state)]; |
25 | 35 | ||
26 | if (!url) { | 36 | if (!url) { |
27 | - url = self.imageURLStorage[@(UIControlStateNormal)]; | 37 | + url = self.imageURLStorage[imageURLKeyForState(UIControlStateNormal)]; |
28 | } | 38 | } |
29 | 39 | ||
30 | return url; | 40 | return url; |
31 | } | 41 | } |
32 | 42 | ||
33 | - (nullable NSURL *)sd_imageURLForState:(UIControlState)state { | 43 | - (nullable NSURL *)sd_imageURLForState:(UIControlState)state { |
34 | - return self.imageURLStorage[@(state)]; | 44 | + return self.imageURLStorage[imageURLKeyForState(state)]; |
35 | } | 45 | } |
36 | 46 | ||
37 | -#pragma mark - Image | ||
38 | - | ||
39 | - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { | 47 | - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { |
40 | [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; | 48 | [self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; |
41 | } | 49 | } |
@@ -62,11 +70,11 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; | @@ -62,11 +70,11 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; | ||
62 | options:(SDWebImageOptions)options | 70 | options:(SDWebImageOptions)options |
63 | completed:(nullable SDExternalCompletionBlock)completedBlock { | 71 | completed:(nullable SDExternalCompletionBlock)completedBlock { |
64 | if (!url) { | 72 | if (!url) { |
65 | - [self.imageURLStorage removeObjectForKey:@(state)]; | 73 | + [self.imageURLStorage removeObjectForKey:imageURLKeyForState(state)]; |
66 | return; | 74 | return; |
67 | } | 75 | } |
68 | 76 | ||
69 | - self.imageURLStorage[@(state)] = url; | 77 | + self.imageURLStorage[imageURLKeyForState(state)] = url; |
70 | 78 | ||
71 | __weak typeof(self)weakSelf = self; | 79 | __weak typeof(self)weakSelf = self; |
72 | [self sd_internalSetImageWithURL:url | 80 | [self sd_internalSetImageWithURL:url |
@@ -82,6 +90,20 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; | @@ -82,6 +90,20 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; | ||
82 | 90 | ||
83 | #pragma mark - Background image | 91 | #pragma mark - Background image |
84 | 92 | ||
93 | +- (nullable NSURL *)sd_currentBackgroundImageURL { | ||
94 | + NSURL *url = self.imageURLStorage[backgroundImageURLKeyForState(self.state)]; | ||
95 | + | ||
96 | + if (!url) { | ||
97 | + url = self.imageURLStorage[backgroundImageURLKeyForState(UIControlStateNormal)]; | ||
98 | + } | ||
99 | + | ||
100 | + return url; | ||
101 | +} | ||
102 | + | ||
103 | +- (nullable NSURL *)sd_backgroundImageURLForState:(UIControlState)state { | ||
104 | + return self.imageURLStorage[backgroundImageURLKeyForState(state)]; | ||
105 | +} | ||
106 | + | ||
85 | - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { | 107 | - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { |
86 | [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; | 108 | [self sd_setBackgroundImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil]; |
87 | } | 109 | } |
@@ -108,11 +130,11 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; | @@ -108,11 +130,11 @@ typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; | ||
108 | options:(SDWebImageOptions)options | 130 | options:(SDWebImageOptions)options |
109 | completed:(nullable SDExternalCompletionBlock)completedBlock { | 131 | completed:(nullable SDExternalCompletionBlock)completedBlock { |
110 | if (!url) { | 132 | if (!url) { |
111 | - [self.imageURLStorage removeObjectForKey:@(state)]; | 133 | + [self.imageURLStorage removeObjectForKey:backgroundImageURLKeyForState(state)]; |
112 | return; | 134 | return; |
113 | } | 135 | } |
114 | 136 | ||
115 | - self.imageURLStorage[@(state)] = url; | 137 | + self.imageURLStorage[backgroundImageURLKeyForState(state)] = url; |
116 | 138 | ||
117 | __weak typeof(self)weakSelf = self; | 139 | __weak typeof(self)weakSelf = self; |
118 | [self sd_internalSetImageWithURL:url | 140 | [self sd_internalSetImageWithURL:url |
-
Please register or login to post a comment