Javascript 如何将图像从expo图像选择器保存到expo文件系统,然后进行渲染?

Javascript 如何将图像从expo图像选择器保存到expo文件系统,然后进行渲染?,javascript,image,react-native,mobile,expo,Javascript,Image,React Native,Mobile,Expo,我试图在应用程序中存储选定的图像,而不是在图像卷中 以下是我尝试过的: 等待文件系统。下载异步( imageUri,//来自expo图像选择器的图像uri FileSystem.documentDirectory+`${uuid}-image.jpg` ) .然后({uri})=>{ log(“已完成下载到”,uri); }) .catch((错误)=>{ 控制台错误(error); }); 我收到错误消息: Unable to download file: Error Domain=NSU

我试图在应用程序中存储选定的图像,而不是在图像卷中

以下是我尝试过的:


等待文件系统。下载异步(
imageUri,//来自expo图像选择器的图像uri
FileSystem.documentDirectory+`${uuid}-image.jpg`
)
.然后({uri})=>{
log(“已完成下载到”,uri);
})
.catch((错误)=>{
控制台错误(error);
});
我收到错误消息:

Unable to download file: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
我还尝试:

await FileSystem.writeastringasync(
FileSystem.documentDirectory+`spotPhotos/${uuid}-image.jpg`,
image.base64
);
当我尝试在ImageBackground组件中使用图像时,这似乎成功地保存了图像,但没有成功


<ImageBackground
        source={'data:image/png;base64'+imageFile}
        style={{ borderRadius: 5, borderColor:  'black', width: 100, flex: 1, resizeMode: "cover", justifyContent: "center" }}
      >
...
</ImageBackground>
我可以使用uri保存图像文件本身吗?我需要将其转换为base64并返回吗


似乎我已经能够成功地保存使用以下代码编码的图像base64:

await FileSystem.writeastringasync(
FileSystem.documentDirectory+`spotPhotos/${uuid}-imagelocation.jpg`,
image.base64
);
并通过以下方式访问编码图像:

let imageFile=async()=>{
让uri=FileSystem.documentDirectory+“spotPhotos/”+spot.imageloc;
let options={encoding:FileSystem.EncodingType.Base64};
让base64=await FileSystem.readAsStringAsync(uri,选项);
返回(base64);
}
当我使用console.log imageFile时,我得到了一个巨大的字符墙,这会导致Vscodium崩溃,即使我尝试使用string.prototype.slice()记录前几个字符,所以我无法检查它,但我认为这是base64编码的文件

当我尝试引用返回值作为图像或ImageBackground组件的源时,如下所示:


//或
//或
//或
我收到警告消息:
为“Image”提供的道具“source”无效。

我还收到一条错误消息

Error: You attempted to set the key `_65` with the value of 1 on an object 
that is meant to be immutable and has been frozen.
由于中的建议不起作用,我的问题可能是从文件中提取的数据


在expo文件系统中存储和访问jpg文件的正确api用法是什么?

我是通过以下代码实现的:

useEffect(() => {
    if (data[spotIndex].image) {
      imageFile();
    }
  }, [location]);

  let imageFile = async () => {
    if (data[spotIndex].image.length > 0) {
      let uri = FileSystem.documentDirectory + data[spotIndex].image;
      let getInfo = await FileSystem.getInfoAsync(uri);
      getInfo && console.log(getInfo);
      let options = { encoding: FileSystem.EncodingType.Base64 };
      let base64 = await FileSystem.readAsStringAsync(uri, options);
      setBase64Val("data:image/jpeg;base64," + base64);
    }
  };


我仍然没有任何运气让它正常工作。甚至制作一个只有这个功能的独立虚拟应用程序。是否确实正确使用了
writeastringasync
?这还需要一个options参数,该参数要求您指定编码。
<ImageBackground source={{ uri: base64Val }}>