我正在构建一个 React Native 应用程序,需要以 Base64 字符串格式存储图像以实现离线查看功能。
什么库/函数可以给我将图像存储为 base64 字符串的最佳结果?假设我的网址是“http://www.example.com/image.png http://www.example.com/image.png".
另外,在将其存储为字符串之前,我是否需要发出http请求来获取它?我的逻辑说是的,但在本机反应中,您可以在 组件上加载图像,而无需先从服务器请求它们。
在本机反应中执行此操作的最佳选择是什么?
I use rn 获取 blob https://github.com/joltup/rn-fetch-blob,基本上它提供了大量的文件系统和网络功能,使数据传输变得非常容易。
反应本机获取 blob https://github.com/wkh237/react-native-fetch-blob已弃用
import RNFetchBlob from "rn-fetch-blob";
const fs = RNFetchBlob.fs;
let imagePath = null;
RNFetchBlob.config({
fileCache: true
})
.fetch("GET", "http://www.example.com/image.png")
// the image is now dowloaded to device's storage
.then(resp => {
// the image path you can use it directly with Image component
imagePath = resp.path();
return resp.readFile("base64");
})
.then(base64Data => {
// here's base64 encoded image
console.log(base64Data);
// remove the file from storage
return fs.unlink(imagePath);
});
source 项目维基 https://github.com/wkh237/react-native-fetch-blob/wiki/Fetch-API
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)