Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ios): optimize method loadImage:completed: #3626

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 8 additions & 1 deletion ios/sdk/component/image/HippyImageViewCustomLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,20 @@ typedef void(^HippyImageLoaderCompletionBlock)(NSData *_Nullable data,
UIImage *_Nullable image,
HippyImageCustomLoaderControlOptions options);

typedef void(^HippyImageLoaderLoadURLCompletionBlock)(NSData *_Nullable data,
NSURL * _Nonnull url,
NSError *_Nullable error,
UIImage *_Nullable image,
BOOL cached);

@protocol HippyImageViewCustomLoader <HippyBridgeModule>

@optional

- (BOOL)canHandleImageURL:(NSURL *)url;

+ (NSData *)convertImageToData:(UIImage *)image;

@required

- (void)imageView:(HippyImageView *)imageView
Expand All @@ -57,7 +64,7 @@ typedef void(^HippyImageLoaderCompletionBlock)(NSData *_Nullable data,

- (void)cancelImageDownload:(UIImageView *)imageView withUrl:(NSURL *)url;

- (void)loadImage:(NSURL *)url completed:(void (^)(NSData *, NSURL *, NSError *, BOOL cached))completedBlock;
- (void)loadImage:(NSURL *)url completed:(HippyImageLoaderLoadURLCompletionBlock)completedBlock;

@end

Expand Down
6 changes: 3 additions & 3 deletions ios/sdk/component/view/HippyBackgroundImageCacheManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ - (void)imageWithUrl:(NSString *)uri completionHandler:(HippyBackgroundImageComp
}
id<HippyImageViewCustomLoader> imageLoader = self.bridge.imageLoader;
if (imageLoader) {
[imageLoader loadImage:imageURL completed:^(NSData *imgData, NSURL *url, NSError *error, BOOL cached) {
UIImage *image = [UIImage imageWithData:imgData scale:[UIScreen mainScreen].scale];
completionHandler(image, error);
[imageLoader loadImage:imageURL completed:^(NSData *imgData, NSURL *url, NSError *error, UIImage *image, BOOL cached) {
UIImage *img = image ?: [UIImage imageWithData:imgData scale:[UIScreen mainScreen].scale];
completionHandler(img, error);
}];
} else {
if ([uri hasPrefix:@"http://"] || [uri hasPrefix:@"https://"]) {
Expand Down
27 changes: 23 additions & 4 deletions ios/sdk/module/image loader/HippyImageLoaderModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,17 @@ @implementation HippyImageLoaderModule
};

if (_bridge.imageLoader && [_bridge.imageLoader respondsToSelector: @selector(loadImage:completed:)]) {
[_bridge.imageLoader loadImage: source_url completed:^(NSData *data, NSURL *url, NSError *error, BOOL cached) {
completedBlock(cached, data, url, error);
__weak typeof(self) weakSelf = self;
[_bridge.imageLoader loadImage: source_url completed:^(NSData *data, NSURL *url, NSError *error, UIImage *image, BOOL cached) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (data) {
completedBlock(cached, data, url, error);
return;
}

if (image && [strongSelf.bridge.imageLoader respondsToSelector: @selector(convertImageToData:)]) {
completedBlock(cached, [strongSelf.bridge.imageLoader convertImageToData:image], url, error);
}
}];
} else {
[[[NSURLSession sharedSession] dataTaskWithURL:source_url completionHandler:^(NSData * _Nullable data, __unused NSURLResponse * _Nullable response, NSError * _Nullable error) {
Expand Down Expand Up @@ -104,8 +113,18 @@ @implementation HippyImageLoaderModule
};

if (_bridge.imageLoader && [_bridge.imageLoader respondsToSelector: @selector(loadImage:completed:)]) {
[_bridge.imageLoader loadImage: source_url completed:^(NSData *data, NSURL *url, NSError *error, BOOL cached) {
completedBlock(cached, data);
__weak typeof(self) weakSelf = self;
[_bridge.imageLoader loadImage: source_url completed:^(NSData *data, NSURL *url, NSError *error, UIImage *image, BOOL cached) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (data) {
completedBlock(cached, data);
return;
}

if (image && [strongSelf.bridge.imageLoader respondsToSelector: @selector(convertImageToData:)]) {
completedBlock(cached, [strongSelf.bridge.imageLoader convertImageToData:image]);
}

}];
} else {
[[[NSURLSession sharedSession] dataTaskWithURL:source_url completionHandler:^(NSData * _Nullable data, __unused NSURLResponse * _Nullable response, NSError * _Nullable error) {
Expand Down
Loading