在 React Native 中加载图像时如何获得响应 header?

How to get a response header when load an image in react native?

相同的url会return不同的图片(随机),我需要得到响应的header(每次也会不同),所以我不能获取两次。

我尝试使用 blob,但收到警告说 'blob' is undefined,代码如下:

let response = await fetch(URLs.host + URLs.imageCode);
let key = response.headers.get('key');
console.log(response.blob); // this will print 'undefined'
let blob = await response.blob();
this.setState({source: URL.createObjectURL(blob)});

...

<Image source={{uri: this.state.source}} />

那么加载图片时如何获取header?

好吧,我发现现在我们无法在 react-native 中使用 {{uri: xxx}} 加载图像时获取上下文,所以我的解决方案是与服务器开发人员交谈,让他们设计 API换一种方式。

根据 this,您不能使用 fetch 在 React-Native 中获取 blob,因为 fetch 是一个本地 http 调用,它将 blob 存储在本机级别并且不提供对实际对象的引用javaScript blob 对象在 res.blob()

中的内存

在有人为此编写桥梁之前,最好的选择是使用 base64 编码的图像,它只是一个字符串,因此您可以通过 res.text()

获取它