简体   繁体   English

如何显示从 fetch() 方法返回的图像 - react-native

[英]How to display image returned from fetch() method - react-native

I have the following code to retrieve an image from an API我有以下代码可以从 API 中检索图像

await fetch('https://example.com/api/user/fileaccess', {
    method: 'POST',
    headers: {
      Accept: '*',
      'Content-Type': 'application/json',
      'token':'xxxxxxxxxxxxxxxxxxxxxxxx'
    },
    body:  JSON.stringify({"applicantid": xxxx, "user_secret": "xxxxxxxxxxx","fileid":xxxxx}),
  }).then((response) => {
    console.log(response);
  }); 

The request was completed and i got log in my console.请求已完成,我登录了我的控制台。

{"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "xxxxx-xxxx-xxx-xxxx-xxxxx", "offset": 0, "size": 953328}}, "_bodyInit": {"_data": {"__collector": [Object], "blobId": "xxxxx-xxxx-xxxx-xxxx-xxxxx", "offset": 0, "size": 953328}}, "headers": {"map": {"cache-control": "max-age=2592000", "connection": "Keep-Alive", "content-type": "image/jpeg", "date": "Tue, 04 Feb 2020 10:34:14 GMT", "expires": "Thu, 05 Mar 2020 10:34:14 GMT", "keep-alive": "timeout=5, max=100", "pragma": "public", "server": "Apache", "transfer-encoding": "chunked"}}, "ok": true, "status": 200, "statusText": undefined, "type": "default", "url": " https://example.com/api/user/fileaccess "} {"_bodyBlob": {"_data": {"__collector": [Object], "blobId": "xxxxx-xxxx-xxx-xxxx-xxxxx", "offset": 0, "size": 953328}}, " _bodyInit": {"_data": {"__collector": [Object], "blobId": "xxxxx-xxxx-xxxx-xxxx-xxxxx", "offset": 0, "size": 953328}}, "headers" : {"map": {"cache-control": "max-age=2592000", "connection": "Keep-Alive", "content-type": "image/jpeg", "date": "Tue, 2020 年 2 月 4 日 10:34:14 GMT", "expires": "Thu, 05 Mar 2020 10:34:14 GMT", "keep-alive": "timeout=5, max=100", "pragma": " public”,“server”:“Apache”,“transfer-encoding”:“chunked”}},“ok”:true,“status”:200,“statusText”:未定义,“type”:“default”,“ url": " https://example.com/api/user/fileaccess "}

My question is, How to display this data as an image in my app?我的问题是,如何在我的应用程序中将此数据显示为图像? Is it possible to convert a base64 from this data?是否可以从此数据转换 base64?

You need to extract it into json or text or some other data before you can do anything with the response.您需要将其提取到 json 或文本或其他一些数据中,然后才能对响应执行任何操作。

As per Mozilla docs , A basic fetch request is really simple to set up.根据Mozilla 文档,基本的获取请求设置起来非常简单。 Have a look at the following code:看看下面的代码:

fetch('http://example.com/movies.json')
  .then((response) => {
    return response.json();
  })
  .then((myJson) => {
    console.log(myJson);
  });

So, modify your code as below :所以,修改你的代码如下:

fetch('https://example.com/api/user/fileaccess', {
    method: 'POST',
    headers: {
      Accept: '*',
      'Content-Type': 'application/json',
      'token':'xxxxxxxxxxxxxxxxxxxxxxxx'
    },
    body:  JSON.stringify({"applicantid": xxxx, "user_secret": "xxxxxxxxxxx","fileid":xxxxx}),
  })
.then(response => response.json())
.then((result) => {
     console.log("Your intended result is: " , result)
}); 

The response.json() is added just assuming the server is returning a JSON data.假设服务器返回 JSON 数据,添加response.json() If not, you can try the below methods too based on your server implementation.如果没有,您也可以根据您的服务器实现尝试以下方法。

arrayBuffer() blob() json() text() formData() arrayBuffer() blob() json() text() formData()

If that's the image URL you can go ahead as follows Store image in your state如果那是图片 URL,您可以按照以下方式继续在您的州存储图片

this.setState({image:res.url})

and then <Image source={{uri:this.state.image}}/>然后<Image source={{uri:this.state.image}}/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM