If you know the aspect ratio for example, if your image is square you can set either the height or the width to fill the container and get the other to be set by the aspectRatio property
Here is the style if you want the height be set automatically:
{
width: '100%',
height: undefined,
aspectRatio: 1,
}
Note: height must be undefined
Edit (Based on @rob-art's comment):
If your image is a different aspect ratio than the one you want to set in the style you can use resizeMode to control how the image should be displayed. Use resizeMode:'contain' to ensure your image is not cropped.
See documentation for more details
If you know the aspect ratio for example, if your image is square you can set either the height or the width to fill the container and get the other to be set by the aspectRatio property
Here is the style if you want the height be set automatically:
{
width: '100%',
height: undefined,
aspectRatio: 1,
}
Note: height must be undefined
Edit (Based on @rob-art's comment):
If your image is a different aspect ratio than the one you want to set in the style you can use resizeMode to control how the image should be displayed. Use resizeMode:'contain' to ensure your image is not cropped.
See documentation for more details
Set the dimensions to the View and make sure your Image is styled with height and width set to 'undefined' like the example below :
<View style={{width: 10, height:10 }} >
<Image style= {{flex:1 , width: undefined, height: undefined}}
source={require('../yourfolder/yourimage')}
/>
</View>
This will make sure your image scales and fits perfectly into your view.
Videos
image auto fix the View
image: {
flex: 1,
width: null,
height: null,
resizeMode: 'contain'
}
Note that the image must be in a container with a predefined size for that to work
I adjust your answer a bit (shixukai)
image: {
flex: 1,
width: 50,
height: 50,
resizeMode: 'contain' }
When I set to null, the image wont show at all. I set to certain size like 50
» npm install react-native-scalable-image
» npm install react-native-fit-image
Try this:
import React, { Component, PropTypes } from "react";
import { Image } from "react-native";
export default class ScaledImage extends Component {
constructor(props) {
super(props);
this.state = { source: { uri: this.props.uri } };
}
componentWillMount() {
Image.getSize(this.props.uri, (width, height) => {
if (this.props.width && !this.props.height) {
this.setState({
width: this.props.width,
height: height * (this.props.width / width)
});
} else if (!this.props.width && this.props.height) {
this.setState({
width: width * (this.props.height / height),
height: this.props.height
});
} else {
this.setState({ width: width, height: height });
}
});
}
render() {
return (
<Image
source={this.state.source}
style={{ height: this.state.height, width: this.state.width }}
/>
);
}
}
ScaledImage.propTypes = {
uri: PropTypes.string.isRequired,
width: PropTypes.number,
height: PropTypes.number
};
I'm passing the URL as a prop called uri. You can specify your width prop as Dimensions.get('window').width and that should cover it.
Note that this will also work if you know what you want to set the height to and you need to resize the width to maintain the ratio. In that case, you would specify the height prop instead of the width one.
There is a property resizeMode set it to 'contain', this solution works only for local images:
Example:
<Image
source={require('./local_path_to/your_image.png')}
style={{ width: 30 }}
resizeMode="contain"
/>
Source: https://facebook.github.io/react-native/docs/image#resizemode
Edit: The above solution is working fine for me, the resizeMode property is not deprecated and I couldn't find any indications that they are planning to do so. If for some reason the the above solution doesn't work for you, you can calculate the height yourself. Here is an axample:
const Demo = () => {
const scaleHeight = ({ source, desiredWidth }) => {
const { width, height } = Image.resolveAssetSource(source)
return desiredWidth / width * height
}
const imageSource = './local_image.png'
const imageWidth = 150
const imageHeigh = scaleHeight({
source: require(imageSource),
desiredWidth: imageWidth
})
return (
<View style={{
display: 'flex',
flex: 1,
alignItems: 'center',
justifyContent: 'center'
}}>
<Image
source={require(imageSource)}
style={{
borderWidth: 1,
width: imageWidth,
height: imageHeigh
}}
/>
</View>
)
}
The above solution works only for local images. Here is how to do the same for remote images:
const RemoteImage = ({uri, desiredWidth}) => {
const [desiredHeight, setDesiredHeight] = React.useState(0)
Image.getSize(uri, (width, height) => {
setDesiredHeight(desiredWidth / width * height)
})
return (
<Image
source={{uri}}
style={{
borderWidth: 1,
width: desiredWidth,
height: desiredHeight
}}
/>
)
}
const Demo = () => {
return (
<View style={{
flex: 1,
alignItems: 'center',
justifyContent: 'center'
}}>
<RemoteImage
uri="https://via.placeholder.com/350x150"
desiredWidth={200}
/>
</View>
)
}