Last active
December 31, 2017 09:41
-
-
Save luanvuhlu/7996857b5a5f8657df08fb297ce0d10c to your computer and use it in GitHub Desktop.
React native: Display photo with ratio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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