Reputation: 324
I'm trying to display icons in a dynamic (react-native-elements) list from a DB via REST call which delivers the icon as base64 encoded string (i.e. here done with jHipster/swagger).
It has to work for both iOS and Android.
I thought this should be a quite common use case, but it turns out somewhat challenging...
What I tried so far:
static addImageFromBase64(base64ImageData, success, failure)
see here
@platform ios
)var base64Icon = 'data:image/png;base64,iVBORw0KGgoAAAANS ...
<Image source={{uri: base64Icon}} ... />
So is there any good way to get a base64 image string into an Android react-native app?
Or is there a good practice or library to solve this (getting icons dynamically from a DB via REST API into a react-native app) in a complete different way?
Upvotes: 4
Views: 5115
Reputation: 324
In fact it works, I was just always combining some different flaws and also added React native elements to increase confusion...
Thanks to martwetzels for leading me in the right direction with the height/width hint.
If someone else has problems with images and the 'data:'
scheme, these are the main obstacles I was stumbling over:
For React Native <Image>
:
'data:'
in an <Image>
, at least you have to provide width and height in styleImage
component nor the 'data:'
scheme is mentioned anywhereSo this is a working example for a React Native <Image>
:
var base64Icon = 'data:image/png;base64,iVBORw0KGgoAAAANS ...
<Image style={{width: 50, height: 50}} source={{uri: base64Icon}}/>
For images in a react-native-elements <List>
avatar
prop will take an url
- the leftIcon
prop is for icons from an included library like material-icons only - OR for a separate React Native elementavatar
prop can have an optional separate avatarStyle
, but will by default scale the image automaticallyleftIcon
prop with a nested <Image>
will again need a style for the picture to appear, with at least width & heightA working example for React Native Elements <List>
with leftIcon
prop with an <Image>
as element (style
mandatory!):
<List>
{
myItems.map((item, i) => (
<ListItem
key={i}
title={item.name}
leftIcon={<Image style={{width: 35, height: 35}} source={{uri: item.base64Icon}}/>}
/>
))
}
</List>
A working example for React Native Elements <List>
with avatar
prop (no style
, but optional avatarStyle
):
<List>
{
myItems.map((item, i) => (
<ListItem
key={i}
title={item.name}
avatar={{uri: item.base64Icon}}
// optional: avatarStyle={{width: 50, height: 50}}
/>
))
}
</List>
So far this is all pretty ovious, once understood... Sorry for asking this dumb question ;-)
As a result, because all my confusion was related to documentation, I will propose some PRs for both React Native and Elements docs.
Edit: PR is now merged and doc changes regarding images with "data:" uri schema and the need to specify size are public meanwhile! Search for "data:" in:
http://facebook.github.io/react-native/docs/images.html
http://facebook.github.io/react-native/docs/image.html
Upvotes: 4