This is the image component I wrote to solve that issue.
It works with both Image and ImageBackground (with children).
import React, {Component} from 'react';
import resolveAssetSource from 'resolveAssetSource';
import {
Image,
ImageBackground,
View,
} from 'react-native';
export default class ScalableImageComponent extends Component {
constructor(props) {
super(props);
}
render() {
const source = resolveAssetSource(this.props.source);
if (source == null) {
return null;
}
const aspectRatio = source.width / source.height;
const fixedWidth = this.props.width;
const fixedHeight = this.props.height;
const widthCalculated = fixedWidth != null ? fixedWidth : fixedHeight * aspectRatio;
const heightCalculated = fixedHeight != null ? fixedHeight : fixedWidth / aspectRatio;
if (this.props.isBackgroundImage) {
return (
<ImageBackground
source={this.props.source}
style={[
this.props.style,
{
width: widthCalculated,
height: heightCalculated,
}
]}
>
{this.props.children}
</ImageBackground>
);
} else {
return (
<Image
source={this.props.source}
style={[
this.props.style,
{
width: widthCalculated,
height: heightCalculated,
}
]}
/>
);
}
}
}
Use it just like you would use a regular image.
If you want it to be an image background just pass a prop isBackgroundImage={true}
eg:
<ScalableImageComponent
isBackgroundImage={true}
source={require('./static.png')}
>
<Text>text over image</Text>
</ScalableImageComponent>