early return when image load canceled

This commit is contained in:
kinarobin 2020-07-16 20:52:15 +08:00 committed by DreamPiggy
parent 4ed1ce29dc
commit 0e0cc576da
1 changed files with 26 additions and 10 deletions

View File

@ -229,18 +229,14 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
} else if ([view isKindOfClass:[UIImageView class]]) { } else if ([view isKindOfClass:[UIImageView class]]) {
UIImageView *imageView = (UIImageView *)view; UIImageView *imageView = (UIImageView *)view;
finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData, SDImageCacheType setCacheType, NSURL *setImageURL) { finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData, SDImageCacheType setCacheType, NSURL *setImageURL) {
if (view.sd_latestOperationKey != nil) { imageView.image = setImage;
imageView.image = setImage;
}
}; };
} }
#if SD_UIKIT #if SD_UIKIT
else if ([view isKindOfClass:[UIButton class]]) { else if ([view isKindOfClass:[UIButton class]]) {
UIButton *button = (UIButton *)view; UIButton *button = (UIButton *)view;
finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData, SDImageCacheType setCacheType, NSURL *setImageURL) { 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 #endif
@ -248,9 +244,7 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
else if ([view isKindOfClass:[NSButton class]]) { else if ([view isKindOfClass:[NSButton class]]) {
NSButton *button = (NSButton *)view; NSButton *button = (NSButton *)view;
finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData, SDImageCacheType setCacheType, NSURL *setImageURL) { finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData, SDImageCacheType setCacheType, NSURL *setImageURL) {
if (view.sd_latestOperationKey != nil) { button.image = setImage;
button.image = setImage;
}
}; };
} }
#endif #endif
@ -258,22 +252,38 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
if (transition) { if (transition) {
#if SD_UIKIT #if SD_UIKIT
[UIView transitionWithView:view duration:0 options:0 animations:^{ [UIView transitionWithView:view duration:0 options:0 animations:^{
if (!view.sd_latestOperationKey) {
return;
}
// 0 duration to let UIKit render placeholder and prepares block // 0 duration to let UIKit render placeholder and prepares block
if (transition.prepares) { if (transition.prepares) {
transition.prepares(view, image, imageData, cacheType, imageURL); transition.prepares(view, image, imageData, cacheType, imageURL);
} }
} completion:^(BOOL finished) { } completion:^(BOOL finished) {
[UIView transitionWithView:view duration:transition.duration options:transition.animationOptions animations:^{ [UIView transitionWithView:view duration:transition.duration options:transition.animationOptions animations:^{
if (!view.sd_latestOperationKey) {
return;
}
if (finalSetImageBlock && !transition.avoidAutoSetImage) { if (finalSetImageBlock && !transition.avoidAutoSetImage) {
finalSetImageBlock(image, imageData, cacheType, imageURL); finalSetImageBlock(image, imageData, cacheType, imageURL);
} }
if (transition.animations) { if (transition.animations) {
transition.animations(view, image); transition.animations(view, image);
} }
} completion:transition.completion]; } completion:^(BOOL finished) {
if (!view.sd_latestOperationKey) {
return;
}
if (transition.completion) {
transition.completion(finished);
}
}];
}]; }];
#elif SD_MAC #elif SD_MAC
[NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull prepareContext) { [NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull prepareContext) {
if (!view.sd_latestOperationKey) {
return;
}
// 0 duration to let AppKit render placeholder and prepares block // 0 duration to let AppKit render placeholder and prepares block
prepareContext.duration = 0; prepareContext.duration = 0;
if (transition.prepares) { if (transition.prepares) {
@ -281,6 +291,9 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
} }
} completionHandler:^{ } completionHandler:^{
[NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) { [NSAnimationContext runAnimationGroup:^(NSAnimationContext * _Nonnull context) {
if (!view.sd_latestOperationKey) {
return;
}
context.duration = transition.duration; context.duration = transition.duration;
#pragma clang diagnostic push #pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations" #pragma clang diagnostic ignored "-Wdeprecated-declarations"
@ -302,6 +315,9 @@ const int64_t SDWebImageProgressUnitCountUnknown = 1LL;
transition.animations(view, image); transition.animations(view, image);
} }
} completionHandler:^{ } completionHandler:^{
if (!view.sd_latestOperationKey) {
return;
}
if (transition.completion) { if (transition.completion) {
transition.completion(YES); transition.completion(YES);
} }