From ec3b0590b07c81f1d2eac94e2cf24a18ea6e0960 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 2 Apr 2019 19:44:23 +0800 Subject: [PATCH 1/3] Cancel latest image load when call cancelCurrentImageLoad --- Examples/SDWebImage Demo/MasterViewController.m | 1 + SDWebImage/UIView+WebCache.m | 11 ++++++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/Examples/SDWebImage Demo/MasterViewController.m b/Examples/SDWebImage Demo/MasterViewController.m index fcedeffe..1abaf90c 100644 --- a/Examples/SDWebImage Demo/MasterViewController.m +++ b/Examples/SDWebImage Demo/MasterViewController.m @@ -112,6 +112,7 @@ [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]] placeholderImage:placeholderImage options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; + [cell.customImageView sd_cancelCurrentImageLoad]; return cell; } diff --git a/SDWebImage/UIView+WebCache.m b/SDWebImage/UIView+WebCache.m index 289c90e3..8f402b47 100644 --- a/SDWebImage/UIView+WebCache.m +++ b/SDWebImage/UIView+WebCache.m @@ -23,6 +23,14 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; objc_setAssociatedObject(self, @selector(sd_imageURL), sd_imageURL, OBJC_ASSOCIATION_RETAIN_NONATOMIC); } +- (nullable NSString *)sd_latestOperationKey { + return objc_getAssociatedObject(self, @selector(sd_latestOperationKey)); +} + +- (void)setSd_latestOperationKey:(NSString * _Nullable)sd_latestOperationKey { + objc_setAssociatedObject(self, @selector(sd_latestOperationKey), sd_latestOperationKey, OBJC_ASSOCIATION_COPY_NONATOMIC); +} + - (NSProgress *)sd_imageProgress { NSProgress *progress = objc_getAssociatedObject(self, @selector(sd_imageProgress)); if (!progress) { @@ -48,6 +56,7 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; if (!validOperationKey) { validOperationKey = NSStringFromClass([self class]); } + [self setSd_latestOperationKey:validOperationKey]; [self sd_cancelImageLoadOperationWithKey:validOperationKey]; self.sd_imageURL = url; @@ -171,7 +180,7 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; } - (void)sd_cancelCurrentImageLoad { - [self sd_cancelImageLoadOperationWithKey:NSStringFromClass([self class])]; + [self sd_cancelImageLoadOperationWithKey:[self sd_latestOperationKey]]; } - (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock cacheType:(SDImageCacheType)cacheType imageURL:(NSURL *)imageURL { From 8294d74e93bcc611ab04f70209c62530eb0524cc Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 2 Apr 2019 19:45:40 +0800 Subject: [PATCH 2/3] Remove test code --- Examples/SDWebImage Demo/MasterViewController.m | 1 - 1 file changed, 1 deletion(-) diff --git a/Examples/SDWebImage Demo/MasterViewController.m b/Examples/SDWebImage Demo/MasterViewController.m index 1abaf90c..fcedeffe 100644 --- a/Examples/SDWebImage Demo/MasterViewController.m +++ b/Examples/SDWebImage Demo/MasterViewController.m @@ -112,7 +112,6 @@ [cell.customImageView sd_setImageWithURL:[NSURL URLWithString:self.objects[indexPath.row]] placeholderImage:placeholderImage options:indexPath.row == 0 ? SDWebImageRefreshCached : 0]; - [cell.customImageView sd_cancelCurrentImageLoad]; return cell; } From c369d70f1228c7fc10f92a14c72005eefa345a38 Mon Sep 17 00:00:00 2001 From: zhongwuzw Date: Tue, 2 Apr 2019 19:57:46 +0800 Subject: [PATCH 3/3] Adjust coding style --- SDWebImage/UIView+WebCache.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/SDWebImage/UIView+WebCache.m b/SDWebImage/UIView+WebCache.m index 8f402b47..cb470c6c 100644 --- a/SDWebImage/UIView+WebCache.m +++ b/SDWebImage/UIView+WebCache.m @@ -56,7 +56,7 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; if (!validOperationKey) { validOperationKey = NSStringFromClass([self class]); } - [self setSd_latestOperationKey:validOperationKey]; + self.sd_latestOperationKey = validOperationKey; [self sd_cancelImageLoadOperationWithKey:validOperationKey]; self.sd_imageURL = url; @@ -180,7 +180,7 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; } - (void)sd_cancelCurrentImageLoad { - [self sd_cancelImageLoadOperationWithKey:[self sd_latestOperationKey]]; + [self sd_cancelImageLoadOperationWithKey:self.sd_latestOperationKey]; } - (void)sd_setImage:(UIImage *)image imageData:(NSData *)imageData basedOnClassOrViaCustomSetImageBlock:(SDSetImageBlock)setImageBlock cacheType:(SDImageCacheType)cacheType imageURL:(NSURL *)imageURL {