react / react/react-native

React 74.5: loading images from data: URIs fails on iOS 15, works on 18

Open
#48,494 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Component: Image Issue: Author Provided Repro Newer Patch Available Platform: iOS
Dominant language
C++
Stars
127k
Forks
25.3k
Avg merge
1d 23h
Merged PRs (30d)
4

Description

Description

I think this is related to: https://github.com/facebook/react-native/issues/42234
I tried reproducing it in the Expo Stack, but was not able to do it with the example on the website (https://reactnative.dev/docs/image#src). I can only presume this is because this happens in a native component.
Here is my code that is as close as possible to the error:

            console.log(`image: ${image.substring(0, 50)}...`),
            <TouchableOpacity key={imgIndex} onPress={() => { setSelectedImage(image); setSelectedFileName(fileName); }}>
              <Image source={{ uri: image }} style={globalStyles.imageThumbnail} onError={(error) => console.error(`${error} image: ${image.substring(image.length-50, image.length)}`)} />
            </TouchableOpacity>

Basically what causes the problem is that on iOS 15 (iphone 6s) the data uri that is being loaded by the Image component receives a .png at the end and a file:// at the beginning. As seen here (this is the warning that is presented to me, I think it comes from the component)
image
And this is the console output (note the warning text with the base64 encoded image data ending with .png, whereas the uri parameter given to the Image component does not have it):

rarKc0szkF5Bp5VemorXUBIcBJkhGhB2NwDc7/teeZ7DaCF52NMLw9Ks9AsOHPx%0D%0A58aFAJoJFGcCqwY+56o4C88CtPBMtlNg9TerPI95m2m20gM0IA08V+W5qc9A9ByA%0D%0A9n3gcQCY90iFZ33TVbWu4Ox4Hjyzv77O2PjrBNDCGUpyVZpVn8312JjqXPfdzQBd%0D%0A4RlVl23sHCrQLCgkKG9XFxRuH8raNYiO/xl4FqBRobnht6wdn6l5EM3fzr+l/5s+%0D%0AL4Jnji0D6P8HoPs5K+6F5G8AAAAASUVORK5CYII=.png
 ERROR  [object Object] image: zr+l/5s+
L4Jnji0D6P8HoPs5K+6F5G8AAAAASUVORK5CYII=

On iOS 18 this behavior (adding png and file) does not get triggered.

Edit:
I did some digging, and the function that shows the Could not find image warning from the screenshot, which already contains the malformed URI, does not even get triggered on the newer iOS version. To test this, I adjusted the code for both locations where the error is triggered:

- (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);
  NSString *message = [NSString stringWithFormat:@"CHANGED2_LOGCould not find image %@", imageURL];
  RCTLogWarn(@"%@", message);
  if (image) {
    if (progressHandler) {
      progressHandler(1, 1);
    }
    completionHandler(nil, image);
  } else {
    NSString *message = [NSString stringWithFormat:@"CHANGED2Could not find image %@", imageURL];
    RCTLogWarn(@"%@", message);
    completionHandler(RCTErrorWithMessage(message), nil);
  }

  return nil;
}

and

- (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);
  NSString *message = [NSString stringWithFormat:@"CHANGED_LOGCould not find image %@", imageURL];
  RCTLogWarn(@"%@", message);
  if (image) {
    if (progressHandler) {
      progressHandler(1, 1);
    }
    completionHandler(nil, image);
  } else {
    NSString *message = [NSString stringWithFormat:@"CHANGEDCould not find image %@", imageURL];
    RCTLogWarn(@"%@", message);
    completionHandler(RCTErrorWithMessage(message), nil);
  }

  return nil;
}

For reference, loadImageForURL is the one getting triggered on iOS 15, and no logs are generated on iOS 18.

Steps to reproduce

hand over data uri with base64 encoded image in <Image source={{ uri: image }}

React Native Version

0.74.5

Affected Platforms

Runtime - iOS

Output of npx react-native info
System:
  OS: macOS 15.1.1
  CPU: (14) arm64 Apple M4 Pro
  Memory: 202.11 MB / 48.00 GB
  Shell:
    version: "5.9"
    path: /bin/zsh
Binaries:
  Node:
    version: 23.3.0
    path: /opt/homebrew/bin/node
  Yarn: Not Found
  npm:
    version: 10.9.0
    path: /opt/homebrew/bin/npm
  Watchman:
    version: 2024.12.02.00
    path: /opt/homebrew/bin/watchman
Managers:
  CocoaPods:
    version: 1.16.2
    path: /opt/homebrew/bin/pod
SDKs:
  iOS SDK:
    Platforms:
      - DriverKit 24.2
      - iOS 18.2
      - macOS 15.2
      - tvOS 18.2
      - visionOS 2.2
      - watchOS 11.2
  Android SDK:
    API Levels:
      - "25"
      - "33"
      - "34"
    Build Tools:
      - 34.0.0
      - 35.0.0
    System Images:
      - android-24 | ARM 64 v8a
      - android-35 | Google Play ARM 64 v8a
    Android NDK: Not Found
IDEs:
  Android Studio: 2024.2 AI-242.23339.11.2421.12550806
  Xcode:
    version: 16.2/16C5032a
    path: /usr/bin/xcodebuild
Languages:
  Java:
    version: 17.0.13
    path: /usr/bin/javac
  Ruby:
    version: 2.6.10
    path: /usr/bin/ruby
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.2.0
    wanted: 18.2.0
  react-native:
    installed: 0.74.5
    wanted: 0.74.5
  react-native-macos: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: true
  newArchEnabled: false
iOS:
  hermesEnabled: true
  newArchEnabled: false
Stacktrace or Logs
see in main message, but here:

rarKc0szkF5Bp5VemorXUBIcBJkhGhB2NwDc7/teeZ7DaCF52NMLw9Ks9AsOHPx%0D%0A58aFAJoJFGcCqwY+56o4C88CtPBMtlNg9TerPI95m2m20gM0IA08V+W5qc9A9ByA%0D%0A9n3gcQCY90iFZ33TVbWu4Ox4Hjyzv77O2PjrBNDCGUpyVZpVn8312JjqXPfdzQBd%0D%0A4RlVl23sHCrQLCgkKG9XFxRuH8raNYiO/xl4FqBRobnht6wdn6l5EM3fzr+l/5s+%0D%0AL4Jnji0D6P8HoPs5K+6F5G8AAAAASUVORK5CYII=.png
 ERROR  [object Object] image: zr+l/5s+
L4Jnji0D6P8HoPs5K+6F5G8AAAAASUVORK5CYII=


### Reproducer

https://snack.expo.dev/@btr/image-example

### Screenshots and Videos

_No response_

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the native iOS loadImageForURL implementations and RCTImageFromLocalAssetURL mentioned in the report. Reproduce the data URI case from the linked Expo Snack on iOS 15 and compare it with iOS 18; done means the Image component loads the URI without adding file:// or .png.

Written by the indexing model from the issue text.

Assessment

Tech stack
ios, objective-c, react-native
Domain
mobile
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.