From 634e4f4522554071946690d542dc7a561ced2568 Mon Sep 17 00:00:00 2001 From: DreamPiggy Date: Fri, 19 Jan 2018 18:06:20 +0800 Subject: [PATCH] Fix that reset alternateImage cancel the image load operation for NSButton+WebCache --- SDWebImage/NSButton+WebCache.h | 12 ++++++++++++ SDWebImage/NSButton+WebCache.m | 20 +++++++++++++++++--- SDWebImage/UIButton+WebCache.h | 2 +- SDWebImage/UIButton+WebCache.m | 4 +++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/SDWebImage/NSButton+WebCache.h b/SDWebImage/NSButton+WebCache.h index 56b45a56..42c3fc69 100644 --- a/SDWebImage/NSButton+WebCache.h +++ b/SDWebImage/NSButton+WebCache.h @@ -232,6 +232,18 @@ progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock; +#pragma mark - Cancel + +/** + * Cancel the current image download + */ +- (void)sd_cancelCurrentImageLoad; + +/** + * Cancel the current alternateImage download + */ +- (void)sd_cancelCurrentAlternateImageLoad; + @end #endif diff --git a/SDWebImage/NSButton+WebCache.m b/SDWebImage/NSButton+WebCache.m index ead14f35..5a012c36 100644 --- a/SDWebImage/NSButton+WebCache.m +++ b/SDWebImage/NSButton+WebCache.m @@ -10,6 +10,7 @@ #if SD_MAC +#import "UIView+WebCacheOperation.h" #import "UIView+WebCache.h" @implementation NSButton (WebCache) @@ -45,11 +46,14 @@ options:(SDWebImageOptions)options progress:(nullable SDWebImageDownloaderProgressBlock)progressBlock completed:(nullable SDExternalCompletionBlock)completedBlock { + __weak typeof(self)weakSelf = self; [self sd_internalSetImageWithURL:url placeholderImage:placeholder options:options - operationKey:nil - setImageBlock:nil + operationKey:@"NSButtonImageOperation" + setImageBlock:^(NSImage * _Nullable image, NSData * _Nullable imageData) { + weakSelf.image = image; + } progress:progressBlock completed:completedBlock]; } @@ -89,7 +93,7 @@ [self sd_internalSetImageWithURL:url placeholderImage:placeholder options:options - operationKey:nil + operationKey:@"NSButtonAlternateImageOperation" setImageBlock:^(NSImage * _Nullable image, NSData * _Nullable imageData) { weakSelf.alternateImage = image; } @@ -97,6 +101,16 @@ completed:completedBlock]; } +#pragma mark - Cancel + +- (void)sd_cancelCurrentImageLoad { + [self sd_cancelImageLoadOperationWithKey:@"NSButtonImageOperation"]; +} + +- (void)sd_cancelCurrentAlternateImageLoad { + [self sd_cancelImageLoadOperationWithKey:@"NSButtonAlternateImageOperation"]; +} + @end #endif diff --git a/SDWebImage/UIButton+WebCache.h b/SDWebImage/UIButton+WebCache.h index b6c4a2a2..61fada62 100644 --- a/SDWebImage/UIButton+WebCache.h +++ b/SDWebImage/UIButton+WebCache.h @@ -128,7 +128,7 @@ options:(SDWebImageOptions)options completed:(nullable SDExternalCompletionBlock)completedBlock; -#pragma mark - Background image +#pragma mark - Background Image /** * Get the current background image URL. diff --git a/SDWebImage/UIButton+WebCache.m b/SDWebImage/UIButton+WebCache.m index 11e34b18..83aebd04 100644 --- a/SDWebImage/UIButton+WebCache.m +++ b/SDWebImage/UIButton+WebCache.m @@ -87,7 +87,7 @@ static inline NSString * backgroundImageURLKeyForState(UIControlState state) { completed:completedBlock]; } -#pragma mark - Background image +#pragma mark - Background Image - (nullable NSURL *)sd_currentBackgroundImageURL { NSURL *url = self.imageURLStorage[backgroundImageURLKeyForState(self.state)]; @@ -146,6 +146,8 @@ static inline NSString * backgroundImageURLKeyForState(UIControlState state) { completed:completedBlock]; } +#pragma mark - Cancel + - (void)sd_cancelImageLoadForState:(UIControlState)state { [self sd_cancelImageLoadOperationWithKey:[NSString stringWithFormat:@"UIButtonImageOperation%@", @(state)]]; }