React Native 无法为 Image.source iOS 使用本地 tmp 文件路径

Asi*_*ora 6 javascript ios reactjs react-native

无法为 Image.source iOS 使用本地临时文件路径。

    <Image 
      style={styles.containerRightCakeImageTag}
      source={{ 
        isStatic: true,
        uri: "file:///Users/md_007/Library/Developer/CoreSimulator/Devices/B450EF4D-7A43-4F2E-AE39-7D92427EBC7E/data/Containers/Data/Application/C2204990-8808-43C0-9678-F6A9CBA74998/Documents/6b66bfab94acc2d89d4c533a1d7ba767.jpeg" 
      }}
    />
Run Code Online (Sandbox Code Playgroud)

Pra*_*bhu 9

您可以使用 react-native-fs 来获取目录(适用于 ios 和 android)

var RNFS = require('react-native-fs');
Run Code Online (Sandbox Code Playgroud)

图像元素看起来像:

<Image source={{uri: 'file://' + RNFS.DocumentDirectoryPath + '/directory/my.png'}} /> 
Run Code Online (Sandbox Code Playgroud)


小智 5

当您读取本地文件或图像(例如使用react-native-cached-image缓存的图像)时,您必须添加“file://”仅适用于Android平台,而不适用于iOS平台。

这是我的一个应用程序中的一些代码:

let path = Platform.OS === "android" ? ('file://' + imageLocalUrl) : imageLocalUrl;
Run Code Online (Sandbox Code Playgroud)

但是,您最好使用“react-native-fs”来读取文件路径,而不是像代码中那样使用模拟器路径。