From 34ce4eb72a4479cd6cba1edadaf89fd894af0905 Mon Sep 17 00:00:00 2001 From: mengyanluo Date: Thu, 19 Nov 2020 11:28:12 +0800 Subject: [PATCH] feat(ios): check necessity of reloading image for HippyImageView --- ios/sdk/component/image/HippyImageView.m | 29 ++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/ios/sdk/component/image/HippyImageView.m b/ios/sdk/component/image/HippyImageView.m index 02ed6e41d..36db175cc 100644 --- a/ios/sdk/component/image/HippyImageView.m +++ b/ios/sdk/component/image/HippyImageView.m @@ -140,6 +140,9 @@ UIImage *HippyBlurredImageWithRadiusv(UIImage *inputImage, CGFloat radius) } @property (nonatomic) HippyAnimatedImageOperation *animatedImageOperation; +@property (atomic, strong) NSString *pendingImageSourceUri;// The image source that's being loaded from the network +@property (atomic, strong) NSString *imageSourceUri;// The image source that's currently displayed + @end @implementation HippyImageView @@ -168,9 +171,9 @@ UIImage *HippyBlurredImageWithRadiusv(UIImage *inputImage, CGFloat radius) [super didMoveToWindow]; if (!self.window) { [self cancelImageLoad]; - } else { - [self reloadImage]; - } + } else if ([self shouldChangeImageSource]) { + [self reloadImage]; + } } - (void)didReceiveMemoryWarning { @@ -265,6 +268,20 @@ UIImage *HippyBlurredImageWithRadiusv(UIImage *inputImage, CGFloat radius) } } +- (BOOL)shouldChangeImageSource +{ +// We need to reload if the desired image source is different from the current image +// source AND the image load that's pending + NSDictionary *source = [self.source firstObject]; + if (source) { + NSString *desiredImageSource = source[@"uri"]; + + return ![desiredImageSource isEqual:self.imageSourceUri] && + ![desiredImageSource isEqual:self.pendingImageSourceUri]; + } + return NO; +} + - (void)reloadImage { NSDictionary *source = [self.source firstObject]; @@ -273,7 +290,7 @@ UIImage *HippyBlurredImageWithRadiusv(UIImage *inputImage, CGFloat radius) _onLoadStart(@{}); } NSString *uri = source[@"uri"]; - + self.pendingImageSourceUri = uri; BOOL isBlurredImage = NO; UIImage *image = [[HippyImageCacheManager sharedInstance] loadImageFromCacheForURLString:uri radius:_blurRadius isBlurredImage:&isBlurredImage]; if (image) { @@ -406,6 +423,7 @@ UIImage *HippyBlurredImageWithRadiusv(UIImage *inputImage, CGFloat radius) - (void)cancelImageLoad { + self.pendingImageSourceUri = nil; NSDictionary *source = [self.source firstObject]; if (_bridge.imageLoader) { [_animatedImageOperation cancel]; @@ -426,6 +444,7 @@ UIImage *HippyBlurredImageWithRadiusv(UIImage *inputImage, CGFloat radius) [self cancelImageLoad]; [self.layer removeAnimationForKey:@"contents"]; self.image = nil; + self.imageSourceUri = nil; } - (UIImage *) imageFromData:(NSData *)data { @@ -520,6 +539,8 @@ UIImage *HippyBlurredImageWithRadiusv(UIImage *inputImage, CGFloat radius) __weak typeof(self) weakSelf = self; void (^setImageBlock)(UIImage *) = ^(UIImage *image) { + weakSelf.pendingImageSourceUri = nil; + weakSelf.imageSourceUri = url; if (image.hippyKeyframeAnimation) { [weakSelf.layer addAnimation:image.hippyKeyframeAnimation forKey:@"contents"]; } else {