From 0e0cc576dad409e4b0072b558479f2e997e3ea0f Mon Sep 17 00:00:00 2001 From: kinarobin Date: Thu, 16 Jul 2020 20:52:15 +0800 Subject: [PATCH] early return when image load canceled --- SDWebImage/Core/UIView+WebCache.m | 36 ++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/SDWebImage/Core/UIView+WebCache.m b/SDWebImage/Core/UIView+WebCache.m index fb8c76a1..160a77aa 100644 --- a/SDWebImage/Core/UIView+WebCache.m +++ b/SDWebImage/Core/UIView+WebCache.m @@ -229,18 +229,14 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; } else if ([view isKindOfClass:[UIImageView class]]) { UIImageView *imageView = (UIImageView *)view; finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData, SDImageCacheType setCacheType, NSURL *setImageURL) { - if (view.sd_latestOperationKey != nil) { - imageView.image = setImage; - } + imageView.image = setImage; }; } #if SD_UIKIT else if ([view isKindOfClass:[UIButton class]]) { UIButton *button = (UIButton *)view; finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData, SDImageCacheType setCacheType, NSURL *setImageURL) { - if (view.sd_latestOperationKey != nil) { - [button setImage:setImage forState:UIControlStateNormal]; - } + [button setImage:setImage forState:UIControlStateNormal]; }; } #endif @@ -248,9 +244,7 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; else if ([view isKindOfClass:[NSButton class]]) { NSButton *button = (NSButton *)view; finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData, SDImageCacheType setCacheType, NSURL *setImageURL) { - if (view.sd_latestOperationKey != nil) { - button.image = setImage; - } + button.image = setImage; }; } #endif @@ -258,22 +252,38 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; if (transition) { #if SD_UIKIT [UIView transitionWithView:view duration:0 options:0 animations:^{ + if (!view.sd_latestOperationKey) { + return; + } // 0 duration to let UIKit render placeholder and prepares block if (transition.prepares) { transition.prepares(view, image, imageData, cacheType, imageURL); } } completion:^(BOOL finished) { [UIView transitionWithView:view duration:transition.duration options:transition.animationOptions animations:^{ + if (!view.sd_latestOperationKey) { + return; + } if (finalSetImageBlock && !transition.avoidAutoSetImage) { finalSetImageBlock(image, imageData, cacheType, imageURL); } if (transition.animations) { transition.animations(view, image); } - } completion:transition.completion]; + } completion:^(BOOL finished) { + if (!view.sd_latestOperationKey) { + return; + } + if (transition.completion) { + transition.completion(finished); + } + }]; }]; #elif SD_MAC [NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull prepareContext) { + if (!view.sd_latestOperationKey) { + return; + } // 0 duration to let AppKit render placeholder and prepares block prepareContext.duration = 0; if (transition.prepares) { @@ -281,6 +291,9 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; } } completionHandler:^{ [NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) { + if (!view.sd_latestOperationKey) { + return; + } context.duration = transition.duration; #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wdeprecated-declarations" @@ -302,6 +315,9 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL; transition.animations(view, image); } } completionHandler:^{ + if (!view.sd_latestOperationKey) { + return; + } if (transition.completion) { transition.completion(YES); }