Skip to content

Instantly share code, notes, and snippets.

@luanvuhlu
Last active December 31, 2017 09:41
Show Gist options
  • Save luanvuhlu/7996857b5a5f8657df08fb297ce0d10c to your computer and use it in GitHub Desktop.
Save luanvuhlu/7996857b5a5f8657df08fb297ce0d10c to your computer and use it in GitHub Desktop.
React native: Display photo with ratio
Image.getSize(this.props.image, (srcWidth, srcHeight) => {
const maxHeight = Dimensions.get('window').height;
const maxWidth = Dimensions.get('window').width;
const ratio = Math.min(maxWidth / srcWidth, maxHeight / srcHeight);
this.setState({ width: srcWidth * ratio, height: srcHeight * ratio });
}, error => {
console.log('error:', error);
});
<View style={styles.container}>
<Image source={{uri: this.props.image}}
resizeMode={Image.resizeMode.center}
style={{
width: this.state.width,
height: this.state.height
}} />
</View>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment