React Native iOS从应用程序Documents文件夹中读取图像

我必须将图像下载到我的iOS应用程序本地文件系统并将其读回。

在我的模拟器上,我可以看到我的应用程序的目录文件系统

/Library/Developer/CoreSimulator/Devices/[UUID]/data/Containers/Data/Application/[UUID]/ /Documents /Library /tmp 

尝试我复制foo.png到/ Documents文件夹,然后尝试

 <Image source={{uri: "file://Documents/foo.png"}} style={{width:100, height:100}}/> 

这不起作用,任何想法什么是做到这一点的方法。

这也花了我很多年… …更好的信息,这将是5分钟!

我使用react-native-fs来获取目录(这适用于ios和android):

 var RNFS = require('react-native-fs'); 

RNFS.DocumentDirectoryPath然后在iOS上返回类似“/ var / mobile / Containers / Data / Application / 15AC1CC6-8CFF-48F0-AFFD-949368383ACB / Documents”

我的图像元素看起来像:

 <Image style={{width:100, height:100}} source={{uri: 'file://' + RNFS.DocumentDirectoryPath + '/myAwesomeSubDir/my.png', scale:1}} /> 

我的问题是,我没有设置宽度和高度第一,这是不需要的url或资源的path。 当使用base64 uri时,宽度和高度也是强制性的(这实际上使我find了解决scheme!)。

尝试tilda(〜)。 React Native应该扩展。

“`

 <Image style={{width:100, height:100}} source={{uri: '~/Documents/myAwesomeSubDir/my.png', scale:1}} /> 

“`