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]]) {
UIImageView *imageView = (UIImageView *)view;
finalSetImageBlock = ^(UIImage *setImage, NSData *setImageData, SDImageCacheType setCacheType, NSURL *setImageURL) {
if (view.sd_latestOperationKey != nil) {
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];
}
};
}
#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;
}
};
}
#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);
}