Repository: incubator-weex Updated Branches: refs/heads/master c65acbd84 -> 4b0e37423
[WEEX-239] add more download image interface for imageloader add more download image interface, you can obtain more details such as the progress of downloading image Bug:239 Project: http://git-wip-us.apache.org/repos/asf/incubator-weex/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-weex/commit/c994aa86 Tree: http://git-wip-us.apache.org/repos/asf/incubator-weex/tree/c994aa86 Diff: http://git-wip-us.apache.org/repos/asf/incubator-weex/diff/c994aa86 Branch: refs/heads/master Commit: c994aa86d8335a6c6ea1dd58bcf55296391608a2 Parents: c65acbd Author: acton393 <zhangxing610...@gmail.com> Authored: Tue Mar 6 20:06:17 2018 +0800 Committer: acton393 <zhangxing610...@gmail.com> Committed: Tue Mar 6 20:06:17 2018 +0800 ---------------------------------------------------------------------- .../extend/handler/WXImgLoaderDefaultImpl.m | 18 +++++ .../Sources/Component/WXImageComponent.m | 79 +++++++++++++++----- .../Sources/Protocol/WXImgLoaderProtocol.h | 42 +++++++++++ 3 files changed, 119 insertions(+), 20 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c994aa86/ios/playground/WeexDemo/extend/handler/WXImgLoaderDefaultImpl.m ---------------------------------------------------------------------- diff --git a/ios/playground/WeexDemo/extend/handler/WXImgLoaderDefaultImpl.m b/ios/playground/WeexDemo/extend/handler/WXImgLoaderDefaultImpl.m index 1adaa5e..a085ddb 100644 --- a/ios/playground/WeexDemo/extend/handler/WXImgLoaderDefaultImpl.m +++ b/ios/playground/WeexDemo/extend/handler/WXImgLoaderDefaultImpl.m @@ -60,4 +60,22 @@ }]; } +- (void)setImageViewWithURL:(UIImageView *)imageView url:(NSURL *)url placeholderImage:(UIImage *)placeholder options:(NSDictionary *)options progress:(void (^)(NSInteger, NSInteger))progressBlock completed:(void (^)(UIImage *, NSError *, WXImageLoaderCacheType, NSURL *))completedBlock +{ + SDWebImageOptions sdWebimageOption = SDWebImageRetryFailed; + if (options && options[@"sdWebimageOption"]) { + [options[@"sdWebimageOption"] intValue]; + } + + [imageView sd_setImageWithURL:url placeholderImage:placeholder options:sdWebimageOption progress:^(NSInteger receivedSize, NSInteger expectedSize) { + if (progressBlock) { + progressBlock(receivedSize, expectedSize); + } + } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) { + if (completedBlock) { + completedBlock(image, error, (WXImageLoaderCacheType)cacheType, imageURL); + } + }]; +} + @end http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c994aa86/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m ---------------------------------------------------------------------- diff --git a/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m b/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m index 3f79791..891217a 100644 --- a/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m +++ b/ios/sdk/WeexSDK/Sources/Component/WXImageComponent.m @@ -30,6 +30,8 @@ #import "WXSDKEngine.h" #import "WXUtility.h" #import "WXAssert.h" +#import "WXConfigCenterProtocol.h" +#import "WXSDKEngine.h" #import <pthread/pthread.h> @interface WXImageView : UIImageView @@ -64,6 +66,7 @@ static dispatch_queue_t WXImageUpdateQueue; @property (nonatomic, strong) id<WXImageOperationProtocol> placeholderOperation; @property (nonatomic) BOOL imageLoadEvent; @property (nonatomic) BOOL imageDownloadFinish; +@property (nonatomic) BOOL downloadImageWithURL; @end @@ -101,6 +104,14 @@ WX_EXPORT_METHOD(@selector(save:)) if (attributes[@"quality"]) { _imageQuality = [WXConvert WXImageQuality:attributes[@"quality"]]; } + id<WXConfigCenterProtocol> configCenter = [WXSDKEngine handlerForProtocol:@protocol(WXConfigCenterProtocol)]; + _downloadImageWithURL = YES; + if ([configCenter respondsToSelector:@selector(configForKey:defaultValue:isDefault:)]) { + _downloadImageWithURL = [configCenter configForKey:@"iOS_weex_ext_config.downloadImageWithURL" defaultValue:@(YES) isDefault:NULL]; + } + if (attributes[@"compositing"]) { + _downloadImageWithURL = [WXConvert BOOL:attributes[@"compositing"]]; + } _imageSharp = [WXConvert WXImageSharp:styles[@"sharpen"]]; _imageLoadEvent = NO; @@ -352,27 +363,55 @@ WX_EXPORT_METHOD(@selector(save:)) - (void)updateImage { __weak typeof(self) weakSelf = self; - dispatch_async(WXImageUpdateQueue, ^{ - __strong typeof(weakSelf) strongSelf = weakSelf; - [strongSelf cancelImage]; - - void(^downloadFailed)(NSString *, NSError *) = ^void(NSString *url, NSError *error) { - weakSelf.imageDownloadFinish = YES; - WXLogError(@"Error downloading image: %@, detail:%@", url, [error localizedDescription]); - }; - - [strongSelf updatePlaceHolderWithFailedBlock:downloadFailed]; - [strongSelf updateContentImageWithFailedBlock:downloadFailed]; - - if (!strongSelf.imageSrc && !strongSelf.placeholdSrc) { - dispatch_async(dispatch_get_main_queue(), ^{ - - strongSelf.layer.contents = nil; - strongSelf.imageDownloadFinish = YES; - [strongSelf readyToRender]; - }); + if (_downloadImageWithURL && [[self imageLoader] respondsToSelector:@selector(setImageViewWithURL:url:placeholderImage:options:progress:completed:)]) { + NSString *newURL = nil; + if (self.placeholdSrc) { + newURL = [self.placeholdSrc copy]; + WX_REWRITE_URL([self placeholdSrc], WXResourceTypeImage, self.weexInstance) + [[self imageLoader] setImageViewWithURL:(UIImageView*)self.view url:[NSURL URLWithString:newURL] placeholderImage:nil options:nil progress:nil completed:nil]; } - }); + newURL = [[self imageSrc] copy]; + WX_REWRITE_URL([self imageSrc], WXResourceTypeImage, self.weexInstance) + NSDictionary *userInfo = @{@"imageQuality":@(self.imageQuality), @"imageSharp":@(self.imageSharp), @"blurRadius":@(self.blurRadius)}; + [[self imageLoader] setImageViewWithURL:(UIImageView*)self.view url:[NSURL URLWithString:newURL] placeholderImage:nil options:userInfo progress:^(NSInteger receivedSize, NSInteger expectedSize) { + // progress when loading image + } completed:^(UIImage *image, NSError *error, WXImageLoaderCacheType cacheType, NSURL *imageURL) { + __strong typeof(weakSelf) strongSelf = weakSelf; + if (strongSelf.imageLoadEvent) { + NSMutableDictionary *sizeDict = [NSMutableDictionary new]; + sizeDict[@"naturalWidth"] = @0; + sizeDict[@"naturalHeight"] = @0; + if (!error) { + sizeDict[@"naturalWidth"] = @(image.size.width * image.scale); + sizeDict[@"naturalHeight"] = @(image.size.height * image.scale); + } else { + [sizeDict setObject:[error description]?:@"" forKey:@"errorDesc"]; + } + [strongSelf fireEvent:@"load" params:@{ @"success": error? @false : @true,@"size":sizeDict}]; + } + }]; + } else { + dispatch_async(WXImageUpdateQueue, ^{ + __strong typeof(weakSelf) strongSelf = weakSelf; + [strongSelf cancelImage]; + + void(^downloadFailed)(NSString *, NSError *) = ^void(NSString *url, NSError *error) { + weakSelf.imageDownloadFinish = YES; + WXLogError(@"Error downloading image: %@, detail:%@", url, [error localizedDescription]); + }; + + [strongSelf updatePlaceHolderWithFailedBlock:downloadFailed]; + [strongSelf updateContentImageWithFailedBlock:downloadFailed]; + + if (!strongSelf.imageSrc && !strongSelf.placeholdSrc) { + dispatch_async(dispatch_get_main_queue(), ^{ + strongSelf.layer.contents = nil; + strongSelf.imageDownloadFinish = YES; + [strongSelf readyToRender]; + }); + } + }); + } } - (void)updatePlaceHolderWithFailedBlock:(void(^)(NSString *, NSError *))downloadFailedBlock http://git-wip-us.apache.org/repos/asf/incubator-weex/blob/c994aa86/ios/sdk/WeexSDK/Sources/Protocol/WXImgLoaderProtocol.h ---------------------------------------------------------------------- diff --git a/ios/sdk/WeexSDK/Sources/Protocol/WXImgLoaderProtocol.h b/ios/sdk/WeexSDK/Sources/Protocol/WXImgLoaderProtocol.h index 1dbd188..f7728c5 100644 --- a/ios/sdk/WeexSDK/Sources/Protocol/WXImgLoaderProtocol.h +++ b/ios/sdk/WeexSDK/Sources/Protocol/WXImgLoaderProtocol.h @@ -26,6 +26,21 @@ @end +typedef NS_ENUM(NSInteger, WXImageLoaderCacheType) { + /** + * The image wasn't available the imageLoad caches, but was downloaded from the web. + */ + WXImageLoaderCacheTypeNone, + /** + * The image was obtained from the disk cache. + */ + WXImageLoaderCacheTypeDisk, + /** + * The image was obtained from the memory cache. + */ + WXImageLoaderCacheTypeMemory +}; + @protocol WXImgLoaderProtocol <WXModuleProtocol> /** @@ -44,4 +59,31 @@ */ - (id<WXImageOperationProtocol>)downloadImageWithURL:(NSString *)url imageFrame:(CGRect)imageFrame userInfo:(NSDictionary *)options completed:(void(^)(UIImage *image, NSError *error, BOOL finished))completedBlock; +@optional + +/** + * @abstract Creates a image download handler with a given URL + * + * @param imageView UIImageView to display the image + * + * @param url The URL of the image to download + * + * @param placeholder The image to be set initially, until the image request finishes. + * + * @param options : The options to be used for download operation + * + * @param progress: A block called while the download start + * + * @param completedBlock : A block called once the download is completed. + * image : the image which has been download to local. + * error : the error which has happened in download. + * finished : a Boolean value indicating whether download action has finished. + */ +- (void)setImageViewWithURL:(UIImageView*)imageView + url:(NSURL *)url + placeholderImage:(UIImage *)placeholder + options:(NSDictionary*)options + progress:(void(^)(NSInteger receivedSize, NSInteger expectedSize))progressBlock + completed:(void(^)(UIImage *image, NSError *error, WXImageLoaderCacheType cacheType, NSURL *imageURL))completedBlock; + @end