Permalink
Browse files

Merge pull request #2002 from iwill/master

Fix issue #2001, add sd_currentBackgroundImageURL and sd_backgroundIm…
  • Loading branch information...
2 parents 94cdb77 + de897ae commit e4ab5ebce2450c3ef9094ea93b8170fd130743be @bpoplauschi bpoplauschi committed on GitHub Aug 18, 2017
Showing with 46 additions and 12 deletions.
  1. +14 −2 SDWebImage/UIButton+WebCache.h
  2. +32 −10 SDWebImage/UIButton+WebCache.m
@@ -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.
*
@@ -131,6 +131,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`.
*
* The download is asynchronous and cached.
@@ -16,26 +16,34 @@
static char imageURLStorageKey;
-typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary;
+typedef NSMutableDictionary<NSString *, NSURL *> SDStateImageURLDictionary;
+
+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)
+#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)];
}
-#pragma mark - Image
-
- (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {
[self sd_setImageWithURL:url forState:state placeholderImage:nil options:0 completed:nil];
}
@@ -62,11 +70,11 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url
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
@@ -82,6 +90,20 @@ - (void)sd_setImageWithURL:(nullable NSURL *)url
#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 +130,11 @@ - (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
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

0 comments on commit e4ab5eb

Please sign in to comment.