diff --git a/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m index 70500f4045..1cb824516f 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m +++ b/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m @@ -57,6 +57,7 @@ @interface WXImageComponent () pthread_mutex_t _imageSrcMutex; pthread_mutexattr_t _propertMutexAttr; BOOL _shouldUpdateImage; + BOOL _mainImageSuccess; } @property (atomic, strong) NSString *placeholdSrc; @@ -398,12 +399,30 @@ - (void)updateImage } __weak typeof(self) weakSelf = self; if (_downloadImageWithURL && [[self imageLoader] respondsToSelector:@selector(setImageViewWithURL:url:placeholderImage:options:progress:completed:)]) { + _mainImageSuccess = NO; + NSString *newURL = nil; if (self.placeholdSrc) { newURL = [self.placeholdSrc copy]; WX_REWRITE_URL([self placeholdSrc], WXResourceTypeImage, self.weexInstance) - NSDictionary* extInfo = @{@"instanceId":[self _safeInstanceId]}; - [[self imageLoader] setImageViewWithURL:(UIImageView*)self.view url:[NSURL URLWithString:newURL] placeholderImage:nil options:extInfo progress:nil completed:nil]; + NSDictionary* extInfo = @{@"instanceId":[self _safeInstanceId], @"pageURL": self.weexInstance.scriptURL ?: @""}; + [[self imageLoader] setImageViewWithURL:(UIImageView*)self.view url:[NSURL URLWithString:newURL] placeholderImage:nil options:extInfo progress:nil completed:^(UIImage *image, NSError *error, WXImageLoaderCacheType cacheType, NSURL *imageURL) { + /* We cannot rely on image library even if we call setImage with placeholer before calling setImage with real url. + The placeholder image may be downloaded and decoded after the real url, so finally we show the placeholder image by wrong. */ + __strong typeof(weakSelf) strongSelf = weakSelf; + if (strongSelf) { + UIImageView *imageView = (UIImageView *)strongSelf.view; + if (imageView && imageView.image == image && strongSelf->_mainImageSuccess) { + // reload image with main image url + NSString* newURL = [[strongSelf imageSrc] copy]; + WX_REWRITE_URL([strongSelf imageSrc], WXResourceTypeImage, strongSelf.weexInstance) + NSDictionary *userInfo = @{@"imageQuality":@(strongSelf.imageQuality), @"imageSharp":@(strongSelf.imageSharp), @"blurRadius":@(strongSelf.blurRadius), @"instanceId":[strongSelf _safeInstanceId], @"pageURL": strongSelf.weexInstance.scriptURL ?: @""}; + [[strongSelf imageLoader] setImageViewWithURL:imageView url:[NSURL URLWithString:newURL] placeholderImage:nil options:userInfo progress:nil completed:^(UIImage *image, NSError *error, WXImageLoaderCacheType cacheType, NSURL *imageURL) { + WXLogInfo(@"Image re-requested because placeholder may override main image. %@", imageURL); + }]; + } + } + }]; } newURL = [[self imageSrc] copy]; if ([newURL length] == 0) { @@ -436,6 +455,10 @@ - (void)updateImage completed:nil]; return; } + strongSelf->_mainImageSuccess = NO; + } + else { + strongSelf->_mainImageSuccess = YES; } UIImageView *imageView = (UIImageView *)strongSelf.view; if (imageView && imageView.image != image) {