react / react/react-native

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

未关闭
#48,494 2 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Component: Image Issue: Author Provided Repro Newer Patch Available Platform: iOS
主要语言
C++
星标
127k
派生
25.3k
平均合并
1 天 23 小时
30 天内合并 PR
4

描述

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_

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从报告中提到的原生 iOS loadImageForURL 实现和 RCTImageFromLocalAssetURL 开始。在 iOS 15 上复现链接的 Expo Snack 中的数据 URI 情况,并与 iOS 18 进行比较;当 Image 组件加载 URI 时不添加 file:// 或 .png,即表示完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
ios, objective-c, react-native
领域
mobile
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
需要澄清
新手友好度
35/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。