Fix that reset alternateImage cancel the image load operation for NSButton+WebCache

This commit is contained in:
DreamPiggy 2018-01-19 18:06:20 +08:00
parent 8590388a6e
commit 634e4f4522
4 changed files with 33 additions and 5 deletions

View File

@ -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

View File

@ -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

View File

@ -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.

View File

@ -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)]];
}