Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
#import <React/RCTHTTPRequestHandler.h>
#import <React/RCTImageLoader.h>
#import <React/RCTJSIExecutorRuntimeInstaller.h>
#import <React/RCTLocalAssetImageLoader.h>
#import <React/RCTBundleAssetImageLoader.h>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a huge breaking change and something that will break the internal system badly as it is.

My suggestion here is to:

  1. create a copy of RCTLocalAssetImageLoader and rename it RCTBundleAssetImageLoader
  2. Deprecate the RCTLocalAssetImageLoader for one version, at least, so that other libraries that may use this class won't break in the next release and will have some time to migrate away from it
  3. Replace the usage in the RCTAppSetupUtils as you already did.

In this way, we can bring in the change more easily as it is more conservative. The change will require two passes but it will be much less disruptive.

#import <React/RCTNetworking.h>

// Fabric
Expand Down Expand Up @@ -83,7 +83,7 @@ void RCTAppSetupPrepareApp(UIApplication *application, BOOL turboModuleEnabled)
if (moduleClass == RCTImageLoader.class) {
return [[moduleClass alloc] initWithRedirectDelegate:nil
loadersProvider:^NSArray<id<RCTImageURLLoader>> *(RCTModuleRegistry *moduleRegistry) {
return @[ [RCTLocalAssetImageLoader new] ];
return @[ [RCTBundleAssetImageLoader new] ];
}
decodersProvider:^NSArray<id<RCTImageDataDecoder>> *(RCTModuleRegistry *moduleRegistry) {
return @[ [RCTGIFImageDecoder new] ];
Expand Down
12 changes: 12 additions & 0 deletions packages/react-native/Libraries/Image/RCTBundleAssetImageLoader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTImageURLLoader.h>

@interface RCTBundleAssetImageLoader : NSObject <RCTImageURLLoader>

@end
78 changes: 78 additions & 0 deletions packages/react-native/Libraries/Image/RCTBundleAssetImageLoader.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#import <React/RCTBundleAssetImageLoader.h>

#import <atomic>
#import <memory>

#import <React/RCTUtils.h>
#import <ReactCommon/RCTTurboModule.h>

#import "RCTImagePlugins.h"

@interface RCTBundleAssetImageLoader () <RCTTurboModule>
@end

@implementation RCTBundleAssetImageLoader

RCT_EXPORT_MODULE()

- (BOOL)canLoadImageURL:(NSURL *)requestURL
{
return RCTIsBundleAssetURL(requestURL);
}

- (BOOL)requiresScheduling
{
// Don't schedule this loader on the URL queue so we can load the
// local assets synchronously to avoid flickers.
return NO;
}

- (BOOL)shouldCacheLoadedImages
{
// UIImage imageNamed handles the caching automatically so we don't want
// to add it to the image cache.
return NO;
}

- (nullable RCTImageLoaderCancellationBlock)loadImageForURL:(NSURL *)imageURL
size:(CGSize)size
scale:(CGFloat)scale
resizeMode:(RCTResizeMode)resizeMode
progressHandler:(RCTImageLoaderProgressBlock)progressHandler
partialLoadHandler:(RCTImageLoaderPartialLoadBlock)partialLoadHandler
completionHandler:(RCTImageLoaderCompletionBlock)completionHandler
{
UIImage *image = RCTImageFromLocalAssetURL(imageURL);
if (image) {
if (progressHandler) {
progressHandler(1, 1);
}
completionHandler(nil, image);
} else {
NSString *message = [NSString stringWithFormat:@"Could not find image %@", imageURL];
RCTLogWarn(@"%@", message);
completionHandler(RCTErrorWithMessage(message), nil);
}

return nil;
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
(const facebook::react::ObjCTurboModule::InitParams &)params
{
return nullptr;
}

@end

Class RCTBundleAssetImageLoaderCls(void)
{
return RCTBundleAssetImageLoader.class;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#import <React/RCTImageURLLoader.h>

__deprecated_msg("Use RCTBundleAssetImageLoader instead")
@interface RCTLocalAssetImageLoader : NSObject <RCTImageURLLoader>

@end
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ @implementation RCTLocalAssetImageLoader

- (BOOL)canLoadImageURL:(NSURL *)requestURL
{
return RCTIsLocalAssetURL(requestURL);
return RCTIsBundleAssetURL(requestURL);
}

- (BOOL)requiresScheduling
Expand Down