首页 > 解决方案 > 从 API 获取 image_base64 的 JSON 对象并将其转换为图像

问题描述

我正在尝试使用 GET 方法获取 base64 API 响应并显示图像。

api调用和fetch方法如下:

export default class CardCall extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      isLoading: true,
      dataSource: null,
    };
  }
  componentDidMount() {
    return fetch(`${apiUrl}/card/image?cardtoken=token`, {
      method: "GET",
      headers: {
        "Content-Type": "application/json",
        "x-api-key": apiKey,
        "onmo-api-auth": apiAuth,
      },
    })
      .then((response) => response.json())
      .then((responseJson) => {
        console.log("This should be the card image", responseJson)
        this.setState({  
          isLoading: false,
          dataSource: responseJson.dataSource
        });
      })
      .catch((error) => {
        console.log(error);
      });
  }

然后调用将base64渲染成图像如下 < Image source={{ uri: base64.decode(this.state.dataSource) }} />

API 响应是:

cvv: "cvv"

emboss_name: "name"

epan: "pan"

expiry: "expire"

image_base64: "iVBORw0KGgoAAAANSUhEUgAAAdYAAAEjCAIAAAAAJ7enAACAAElEQVR42uzdd3xVRfo/8EsKXWmhpHdC7wKi0kXpnXTSC71D6EXpEAgp9C4gSBEQxIJ0qQn43Z99XV3XtQJKhxTymzlz…&quot;

token: token

我已经尝试映射和提取身体但无济于事,请你帮帮我

标签: jsonreact-nativeapibase64

解决方案


推荐阅读