How to display image stored in a variable?

Viewed 282

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.

2 Answers

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

You can pass the url to source.

Like

<Image
    source={{uri: img}}
    />
Related