Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在React Native中将base64转换为Blob?_Javascript_React Native - Fatal编程技术网

Javascript 如何在React Native中将base64转换为Blob?

Javascript 如何在React Native中将base64转换为Blob?,javascript,react-native,Javascript,React Native,我将在react native中将b64转换为blob 但是我在atob函数上出错了 这是我的密码 var binary = atob(this.state.avatarSource.uri.split(',')[1]); var byteNumbers = new Array(binary.length); for(var i = 0; i < binary.length; i++) { byteNumbers.push(binary.charCodeAt(i)); } var f

我将在react native中将b64转换为blob

但是我在atob函数上出错了

这是我的密码

var binary = atob(this.state.avatarSource.uri.split(',')[1]);
var byteNumbers = new Array(binary.length);

for(var i = 0; i < binary.length; i++) {
  byteNumbers.push(binary.charCodeAt(i));
}
var file = new Blob([new Uint8Array(byteNumbers)], {type: 'image/jpeg'});
var binary=atob(this.state.avatarSource.uri.split(',')[1]);
var byteNumbers=新数组(binary.length);
对于(var i=0;i

有人知道吗?

我得到了一张图片的底图。之后,我显示它从下面的代码。希望能对你有所帮助

let imgSrc = "data:image/png;base64," + base64ImageData;
<Image source={{uri: imgSrc, scale: 1}} style={{ height: 80, width: 80}}/>
let imgSrc=“data:image/png;base64,”+base64ImageData;

不要使用仅在
调试模式下工作的
atob
btoa

因为当您使用调试模式时,您正在浏览器中运行JS代码(应该是V8),而如果您要在生产模式下运行应用程序,它将使用
JavascriptCore
,它没有
atob
btoa
实现

您可以使用将数据转换为BASE64编码的字符串,但我不确定它是否可以为您创建正确的Blob对象

据我所知,Blob是JS上下文和文件系统之间的桥梁,React Native本身还没有文件系统API,因此您可能会得到一个Blob对象,但它总是空的


如果要从包含数字的数组创建图像,请查看我正在处理的项目,希望它能够解决此类问题:)

您可以尝试
获取

  let blob = await this.base64ToBlob(encoded);
  console.log('btoB64 resp === ', blob);

  async base64ToBlob(encoded) {
    let url = `data:image/jpg;base64,${encoded}`;
    let res = await fetch(url);
    let blob = await res?.blob();
    return blob;
  }

你有没有想过?我也遇到了同样的问题:可能重复的非重复,react native使用
JavascriptCore
,例如,它没有实现
atob
btoa
react native fetch blob提供blob吗?im目前正在使用“react native image picker”,它提供URI和base64。我需要把它转换成水滴。请提供建议。react-native-fetch-blob为我解决了此问题。如果您有一个base64字符串需要发送到http(s)post端点,这似乎是一个很好的解决方案,因为它具有此转换的内置功能。图像是一个特定的组件,它在纯html中看起来如何?@ThunD3eR这是React Native,没有html