Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Fix image placeholder #2564

Merged
merged 2 commits into from
Jun 17, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ @interface WXImageComponent ()
pthread_mutex_t _imageSrcMutex;
pthread_mutexattr_t _propertMutexAttr;
BOOL _shouldUpdateImage;
BOOL _mainImageSuccess;
}

@property (atomic, strong) NSString *placeholdSrc;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down