This commit is contained in:
iwill 2017-08-18 13:55:34 +08:00
parent 80dace4a4a
commit de897ae33a
1 changed files with 12 additions and 8 deletions

View File

@ -16,10 +16,14 @@
static char imageURLStorageKey; static char imageURLStorageKey;
typedef NSMutableDictionary<NSNumber *, NSURL *> SDStateImageURLDictionary; typedef NSMutableDictionary<NSString *, NSURL *> SDStateImageURLDictionary;
static inline NSNumber * backgroundImageURLKeyForState(UIControlState state) { static inline NSString * imageURLKeyForState(UIControlState state) {
return @(NSUIntegerMax - state - 1); 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) @implementation UIButton (WebCache)
@ -27,17 +31,17 @@ static inline NSNumber * backgroundImageURLKeyForState(UIControlState state) {
#pragma mark - Image #pragma mark - Image
- (nullable NSURL *)sd_currentImageURL { - (nullable NSURL *)sd_currentImageURL {
NSURL *url = self.imageURLStorage[@(self.state)]; NSURL *url = self.imageURLStorage[imageURLKeyForState(self.state)];
if (!url) { if (!url) {
url = self.imageURLStorage[@(UIControlStateNormal)]; url = self.imageURLStorage[imageURLKeyForState(UIControlStateNormal)];
} }
return url; return url;
} }
- (nullable NSURL *)sd_imageURLForState:(UIControlState)state { - (nullable NSURL *)sd_imageURLForState:(UIControlState)state {
return self.imageURLStorage[@(state)]; return self.imageURLStorage[imageURLKeyForState(state)];
} }
- (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state { - (void)sd_setImageWithURL:(nullable NSURL *)url forState:(UIControlState)state {
@ -66,11 +70,11 @@ static inline NSNumber * backgroundImageURLKeyForState(UIControlState state) {
options:(SDWebImageOptions)options options:(SDWebImageOptions)options
completed:(nullable SDExternalCompletionBlock)completedBlock { completed:(nullable SDExternalCompletionBlock)completedBlock {
if (!url) { if (!url) {
[self.imageURLStorage removeObjectForKey:@(state)]; [self.imageURLStorage removeObjectForKey:imageURLKeyForState(state)];
return; return;
} }
self.imageURLStorage[@(state)] = url; self.imageURLStorage[imageURLKeyForState(state)] = url;
__weak typeof(self)weakSelf = self; __weak typeof(self)weakSelf = self;
[self sd_internalSetImageWithURL:url [self sd_internalSetImageWithURL:url