Search code examples
chatreact-native-gifted-chat

How to display image stored in a variable?


In gifted chat, there is a image field which can be a URL pointing to a image for display. Here is a message example:

{
    _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',
    },
  },

For a image file stored in a variable img, how can I display it in a chat message? message.image = img did not work.


Solution

  • The following code solves the problem, assuming fileBuffer stores the data stream of the jpeg image file.

    {
        _id: 30,
        createdAt: new Date(),
        image: "data:image/png;base64," + fileBuffer,
        user: {
          _id: 2,
          name: 'React Native',
        },
      },