fix(ios): avoid a thread race for image component

This commit is contained in:
luomy
2022-10-22 14:43:02 +08:00
parent 0f3341c29b
commit 4a17ac1381
+19 -16
View File
@@ -355,23 +355,26 @@ NSError *imageErrorFromParams(NSInteger errorCode, NSString *errorDescription) {
return NO;
}
static inline void decodeAndLoadImageAsync(HippyImageView *imageView, id<HippyImageProviderProtocol> instance,
static void decodeAndLoadImageAsync(HippyImageView *imageView, id<HippyImageProviderProtocol> instance,
NSString *imageUrl, BOOL isAnimatedImage) {
__weak HippyImageView *weakSelf = imageView;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__strong HippyImageView *strongSelf = weakSelf;
if (!strongSelf) {
return;
if (!imageView) {
return;
}
if (isAnimatedImage) {
if (imageView->_animatedImageOperation) {
[imageView->_animatedImageOperation cancel];
}
if (isAnimatedImage) {
if (strongSelf->_animatedImageOperation) {
[strongSelf->_animatedImageOperation cancel];
imageView->_animatedImageOperation = [[HippyAnimatedImageOperation alloc] initWithAnimatedImageProvider:instance
imageView:imageView
imageURL:imageUrl];
[animated_image_queue() addOperation:imageView->_animatedImageOperation];
} else {
__weak HippyImageView *weakSelf = imageView;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
__strong HippyImageView *strongSelf = weakSelf;
if (!strongSelf) {
return;
}
strongSelf->_animatedImageOperation = [[HippyAnimatedImageOperation alloc] initWithAnimatedImageProvider:instance
imageView:strongSelf
imageURL:imageUrl];
[animated_image_queue() addOperation:strongSelf->_animatedImageOperation];
} else {
UIImage *image = [instance image];
if (image) {
[strongSelf loadImage:image url:imageUrl error:nil needBlur:YES needCache:YES];
@@ -380,8 +383,8 @@ static inline void decodeAndLoadImageAsync(HippyImageView *imageView, id<HippyIm
NSError *theError = imageErrorFromParams(ImageDataUnavailable, errorMessage);
[strongSelf loadImage:nil url:imageUrl error:theError needBlur:YES needCache:NO];
}
}
});
});
}
}
- (void)reloadImage {