简体   繁体   English

我的回复包含一个 png,我如何在反应中显示图像,因为它不是 url?

[英]My response contains an png, how can I display the image in react since its not a url?

I have a response containing an image in png format,but I am unable to display it in react since it is not a url pic of response我有一个包含 png 格式图像的响应,但我无法在响应中显示它,因为它不是响应的 url pic

does anyone know how I can convert this png into a URL so I can insert it into my src of img tag?有谁知道如何将这个 png 转换为 URL 以便我可以将它插入到我的 img 标签的 src 中?

If you have the image inside your application ( assets folder or whatever ) you could display it like that如果您的应用程序中有图像(资产文件夹或其他),您可以像这样显示它

<img src={`assets/${coverImage}`}>

Else The image should be sent from the API in the base64 format and then you could simply display it like that.否则图像应该从 API 以 base64 格式发送,然后您可以像这样简单地显示它。

data = "base64 image"
<img src={`data:image/jpeg;base64,${data}`} />

Why not treat imege you need as Bese64 string?为什么不将您需要的 imege 视为 Bese64 字符串?
If you're able to convert you png image to a Base64 string (server side obviously) you can easily use it in img tag as src attribute value.如果您能够将 png 图像转换为 Base64 字符串(显然是服务器端),您可以在 img 标签中轻松地将其用作 src 属性值。

This is something I did when I was doing a similar thing in a React project.这是我在 React 项目中做类似事情时所做的事情。

1) Assuming the following: 1) 假设如下:

  • The response that we get from the API is not BASE64 encoded我们从 API 得到的响应不是 BASE64 编码的
  • The responseType is not specified in the request请求中未指定 responseType
  • A png is the response png 是响应

The response data will look like this:响应数据将如下所示: 在此处输入图像描述

With characters that does not look friendly.使用看起来不友好的字符。

2) With changed responseType 2) 改变 responseType

  • Set responseType: "arraybuffer" in the request在请求中设置responseType: "arraybuffer"

The response will now have arrayBuffer in data of the response响应现在将在响应的数据中包含 arrayBuffer

在此处输入图像描述

The response can then be converted to base64 by然后可以通过以下方式将响应转换为 base64

let base64ImageString = Buffer.from(response.data, 'binary').toString('base64')

If you want to display it on an img tag, then prepend data:image/png;base64, to the Base64 string如果您想在 img 标签上显示它,则将data:image/png;base64 附加到 Base64 字符串

let srcValue = "data:image/png;base64,"+base64ImageString

But in your case, it seems like you are only getting the file name as a string and not the file itself.但是在您的情况下,您似乎只是将文件名作为字符串而不是文件本身。

same in my case.在我的情况下也是如此。 "react": "17.0.1", "react-native": "0.64.2",
my response contains png image(no URL, no base64), I converted my response into base64 as below,我的回复包含 png 图像(没有 URL,没有 base64),我将我的回复转换为 base64,如下所示,

const makeApiRequest = async()=>{
    try {
        const response = await apiCaller('ProfileImage',{"thisID": "ABCD","thatID": "1234"})
        if(response.status === 200){
            const dataResponse = await response.blob()
            let blob = new Blob([dataResponse], { type: "text/plain" });
            var reader = new FileReader();
            reader.readAsDataURL(blob);
            reader.onloadend = function () {
               var base64String = reader.result; 
               setImageFromAPI(base64String.substr(base64String.indexOf(', ') + 1)); //Setting response to hook
            }
        }else{
            console.log(response.status)
        }
    } catch (error) {
        console.log("components/profile/ProfileTimeline.js:-",error)
    }
}

But when I use it in my image tag its shows no image, and i don't know why但是当我在我的图像标签中使用它时,它没有显示图像,我不知道为什么

<Image  source={{uri: `data:image/png;base64,${imagefromAPI}`}}  style={{width:100, height:100}} resizeMode="cover" />

暂无
暂无

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

相关问题 我的响应不是 url,如何使用 base64 显示不是 url 的图像并且响应不包含 bodyBytes? - My response is not an url, How can I display an image which is not url using base64 and response does not contain bodyBytes? 如何在 React-Native 中将 base64 url 转换为图像(png/jpg)文件? - How can I convert a base64 url to an image(png/jpg) file, in React-Native? 如何在 React 中将 ArrayBuffer(PNG 图像)直接渲染为图像? - How can I render an ArrayBuffer (PNG image) directly as an image in React? 如何在 React 中默认显示图像 - How can I display by default image in React 在我使用 axios 进行响应的休息 Web 服务请求中,响应 json 包含 html div 元素。 我怎样才能得到这个元素的值? - In my rest webservice request for react using axios, the response json contains html div element. how can I get the value of this element? 我如何检测来自 URL 的视频或图像,并使用 React 和 Material UI 在单个标签中相应地显示它 - How Can I detect video or image from URL and display it accordingly in a single tag using React and Material UI 如何在反应中读取 PNG 的元数据 - How can I read metadata of PNG in react 如何使用GraphQL显示道具图像? 可以显示图片网址,但不能显示图片本身 - How can I display image from props with GraphQL? Can display image url but not the image itself React SSR:我的组件如何在其渲染方法中获取 URL 搜索 URL 参数 - React SSR : How can my component get the URL search URL parameters in its render method 如何将本地(我的系统)png文件导入React应用程序? - How can I import local(my system) png file into React app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM