Skip to content
Open
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
63 changes: 51 additions & 12 deletions package/ios/RNCSliderComponentView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import <React/RCTBridge+Private.h>
#import "RCTImagePrimitivesConversions.h"
#import <React/RCTImageLoaderProtocol.h>
#import <React/RCTUtils.h>
#import "RCTFabricComponentsPlugins.h"
#import "RNCSlider.h"

Expand Down Expand Up @@ -273,23 +274,61 @@ - (void)updateProps:(const Props::Shared &)props oldProps:(const Props::Shared &
}


// TODO temporarily using bridge, workaround for https://github.com/reactwg/react-native-new-architecture/discussions/31#discussioncomment-2717047, rewrite when Meta comes with a solution.
// Legacy-bridge path first: when the bridge is present, RCTImageLoader provides caching,
// decode-time sizing and custom RCTImageURLLoader schemes. On bridgeless, [RCTBridge currentBridge]
// may be nil or expose an ImageLoader that cannot load
// (https://github.com/reactwg/react-native-new-architecture/discussions/31#discussioncomment-2717047),
// so fall back to fetching the bytes directly with NSURLSession. The fallback covers file://
// (bundled require() assets) and http(s) (Metro in dev, remote images) and honours custom request
// headers; it does not support RCTImageURLLoader plugin schemes (e.g. ph://) or ImageLoader's
// resizing and caching. TODO: replace both paths with the Fabric ImageManager pipeline.
- (void)loadImageFromImageSource:(ImageSource)source completionBlock:(RNCLoadImageCompletionBlock)completionBlock failureBlock:(RNCLoadImageFailureBlock)failureBlock
{
NSString *uri = [[NSString alloc] initWithUTF8String:source.uri.c_str()];
if ((BOOL)uri.length) {
[[[RCTBridge currentBridge] moduleForName:@"ImageLoader"]
loadImageWithURLRequest:NSURLRequestFromImageSource(source)
size:CGSizeMake(source.size.width, source.size.height)
scale:source.scale
clipped:NO
resizeMode:RCTResizeModeCover
progressBlock:nil
partialLoadBlock:nil
completionBlock:completionBlock];
} else {
if (!(BOOL)uri.length) {
failureBlock();
return;
}

NSURLRequest *request = NSURLRequestFromImageSource(source);
CGFloat scale = source.scale > 0 ? source.scale : RCTScreenScale();

void (^loadDirectly)(void) = ^{
[[[NSURLSession sharedSession] dataTaskWithRequest:request
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
UIImage *image = data ? [UIImage imageWithData:data scale:scale] : nil;
// The session queue is a background queue; callers touch UIKit, so hop to main.
dispatch_async(dispatch_get_main_queue(), ^{
if (image) {
completionBlock(nil, image);
} else {
failureBlock();
}
});
}] resume];
};

id<RCTImageLoaderProtocol> imageLoader = [[RCTBridge currentBridge] moduleForName:@"ImageLoader"];
if (!imageLoader) {
loadDirectly();
return;
}

[imageLoader
loadImageWithURLRequest:request
size:CGSizeMake(source.size.width, source.size.height)
scale:source.scale
clipped:NO
resizeMode:RCTResizeModeCover
progressBlock:nil
partialLoadBlock:nil
completionBlock:^(NSError *error, UIImage *image) {
if (image) {
completionBlock(nil, image);
} else {
loadDirectly();
}
}];
}

- (void)setInverted:(BOOL)inverted
Expand Down