如何显示存储在变量中的图像?

问题描述 投票:0回答:1

在有礼貌的聊天中,有一个image字段,它可以是指向要显示的图像的URL。这是一个消息示例:

{
    _id: 30,
    createdAt: new Date(),
    image: 'https://upload.wikimedia.org/wikipedia/commons/thumb/6/6e/Paris_-_Eiffelturm_und_Marsfeld2.jpg/280px-Paris_-_Eiffelturm_und_Marsfeld2.jpg',
    user: {
      _id: 2,
      name: 'React Native',
    },
  },

对于存储在变量img中的图像文件,如何在聊天消息中显示它? message.image = img无效。

chat react-native-gifted-chat
1个回答
0
投票

以下代码解决了这个问题,假设fileBuffer存储jpeg图像文件的数据流。

{
    _id: 30,
    createdAt: new Date(),
    image: "data:image/png;base64," + fileBuffer,
    user: {
      _id: 2,
      name: 'React Native',
    },
  },
© www.soinside.com 2019 - 2024. All rights reserved.