Skip to content

Commit

Permalink
feat(ios): optimize method loadImage:completed:
Browse files Browse the repository at this point in the history
  • Loading branch information
Geor9eLau committed Nov 21, 2023
1 parent fadfc8c commit 108a3de
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 8 deletions.
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

0 comments on commit 108a3de

Please sign in to comment.