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

fix(ios): fix timing issue of backgroundImage prop #3799

Merged
merged 2 commits into from
Mar 28, 2024
Merged
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
30 changes: 23 additions & 7 deletions renderer/native/ios/renderer/component/view/HippyView.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
*/

#import <UIKit/UIKit.h>

#import "HippyComponent.h"
#import "NativeRenderTouchesView.h"

@class HippyGradientObject;

NS_ASSUME_NONNULL_BEGIN

@interface HippyView : NativeRenderTouchesView

/**
Expand All @@ -42,7 +43,7 @@
* get content for layer
* return YES if getting content synchronized,else return NO
*/
- (BOOL)getLayerContentForColor:(UIColor *)color completionBlock:(void (^)(UIImage *))contentBlock;
- (BOOL)getLayerContentForColor:(UIColor *)color completionBlock:(void (^)(UIImage *_Nullable))contentBlock;

/**
* CALayerContents Filter
Expand All @@ -51,6 +52,9 @@
- (CALayerContentsFilter)minificationFilter;
- (CALayerContentsFilter)magnificationFilter;


#pragma mark - Border Related

/**
* Border radii.
*/
Expand Down Expand Up @@ -83,13 +87,25 @@
*/
@property (nonatomic, assign) HippyBorderStyle borderStyle;

/**
* BackgroundImage styles.
*/
@property (nonatomic, strong) UIImage *backgroundImage;
@property (nonatomic, assign) NSUInteger backgroundImageUrlHashValue;

#pragma mark - Background Styles

/// The backgroundImage
@property (nonatomic, strong, nullable) UIImage * backgroundImage;

/// Hash value of Background Image Path,
/// Used to eliminate duplication and ensure timing when updating images.
@property (nonatomic, assign) NSUInteger backgroundImageUrlHash;

/// The fail error of background image if any
@property (nonatomic, strong, nullable) NSError *backgroundImageFailError;

@property (nonatomic, strong) NSString *backgroundSize;
@property (nonatomic, assign) CGFloat backgroundPositionX;
@property (nonatomic, assign) CGFloat backgroundPositionY;
@property (nonatomic, strong) HippyGradientObject *gradientObject;


@end

NS_ASSUME_NONNULL_END
38 changes: 30 additions & 8 deletions renderer/native/ios/renderer/component/view/HippyViewManager.mm
Original file line number Diff line number Diff line change
Expand Up @@ -225,16 +225,26 @@ - (void)measureInAppWindow:(NSNumber *)componentTag
if (json) {
NSString *imagePath = [HippyConvert NSString:json];
// Old background image need to be cleaned up in time due to view's reuse
NSUInteger oldHash = view.backgroundImageUrlHashValue;
NSUInteger oldHash = view.backgroundImageUrlHash;
BOOL shouldLoadImage = NO;
if (oldHash != imagePath.hash) {
if (oldHash > 0) {
view.backgroundImage = nil;
}
view.backgroundImageUrlHashValue = imagePath.hash;
view.backgroundImageUrlHash = imagePath.hash;
shouldLoadImage = YES;
} else if (view.backgroundImageFailError) {
// Same image, check whether the last loading was successful.
// If it failed, load it again.
shouldLoadImage = YES;
}
if (shouldLoadImage) {
view.backgroundImageFailError = nil;
[self loadImageSource:imagePath forView:view];
}
[self loadImageSource:imagePath forView:view];
} else {
view.backgroundImageUrlHashValue = 0;
view.backgroundImageUrlHash = 0;
view.backgroundImageFailError = nil;
view.backgroundImage = defaultView.backgroundImage;
}
}
Expand All @@ -253,25 +263,27 @@ - (void)loadImageSource:(NSString *)path forView:(HippyView *)view {
return;
}
NSString *standardizeAssetUrlString = path;
__weak HippyView *weakView = view;
auto loader = [self.bridge.uiManager VFSUriLoader].lock();
if (!loader) {
return;
}
__weak __typeof(self)weakSelf = self;
__weak HippyView *weakView = view;
loader->RequestUntrustedContent(path, imageLoadOperationQueue(), nil,
^(NSData *data, NSDictionary *userInfo, NSURLResponse *response, NSError *error) {
HippyLogTrace(@"%@ load bgImg finish:%@, hash:%lu record:%lu error?%@",
weakView.hippyTag, path, path.hash, weakView.backgroundImageUrlHash, error.description);
// It is possible for User to return the image directly in userInfo,
// So we need to check and skip the data decoding process if needed.
UIImage *resultImage = userInfo ? userInfo[HippyVFSHandlerUserInfoImageKey] : nil;
if (resultImage) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong HippyView *strongView = weakView;
if (strongView) {
if (strongView && strongView.backgroundImageUrlHash == path.hash) {
strongView.backgroundImage = resultImage;
}
});
} else {
} else if (data) {
__strong __typeof(weakSelf)strongSelf = weakSelf;
HippyBridge *bridge = strongSelf.bridge;
if (bridge) {
Expand All @@ -287,11 +299,21 @@ - (void)loadImageSource:(NSString *)path forView:(HippyView *)view {
UIImage *backgroundImage = [imageProvider image];
dispatch_async(dispatch_get_main_queue(), ^{
HippyView *strongView = weakView;
if (strongView) {
if (strongView && strongView.backgroundImageUrlHash == path.hash) {
// Check the hash value of image's path before assignment
// to avoid situations where the old path callback is later than the new path.
strongView.backgroundImage = backgroundImage;
}
});
}
} else if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
__strong HippyView *strongView = weakView;
if (strongView && strongView.backgroundImageUrlHash == path.hash) {
strongView.backgroundImageFailError = error;
HippyLogError(@"%@ load bgImg error:%@ %@", strongView.hippyTag, path, error.description);
}
});
}
});
}
Expand Down
Loading