'How to convert buffer to image with react native
I am using pouchdb with react native (expo) to store the local data and also the images and when trying to recover the image I get the following data
Object {
"data": Array [
161,
184,
222,
114,
211,
155,
141,
231,
45,
],
"type": "Buffer",
}
I would like to convert this return to an image. I already tried to use methods like this in an attempt to get a base64 and so the image later:
let base64String = new Buffer.from(result).toString("base64")
But what I get in return is this
objectObject
Solution 1:[1]
I had a similar issue that I solved like so.
response
is your API result and then I returned it like this:
return Buffer.from(response.data, 'binary').toString('base64)
Solution 2:[2]
I think this might be because you're result.data
property is already a Buffer array. Try just calling toString
on it directly:
let base64String = result.data.toString('base64');
Btw thanks for this question, I couldn't find an answer for how to convert a Uint8Array into a base64 string anywhere!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | RonE |
Solution 2 | cd3k |