Reputation: 1560
I am not aware of the image ratio uploaded by user upfront but I need to display all images in 4:5 ratio i.e
image width=device window width size
image height=(device window width size)*5/4
I used the above calculation for displaying my image inside container view but the image is cropped. If I use resize mode, the image is distorted.
Code:
<View style={styles.container}>
<Image style={styles.fixedRatio} source={{ uri: this.props.navigation.state.params.uri }}/>
</View>
container: {
flex: 1,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center'
},
fixedRatio: {
marginLeft:-10,
marginRight:-10,
backgroundColor: 'red',
flex: 1,
aspectRatio: 1,
width: deviceWidth,
height: deviceWidth*5/4
},
Edit 1:
As per @marson answer, I changed the code like below
I set background color to differentiate image. Now the images are not get cropped. But doesn't fit inside the view
<View style={{ backgroundColor: 'blue', aspectRatio:4/5, alignSelf:'center', width: '100%'}}>
<Image resizeMode={'contain'} style={{width:'100%', height:'100%'}} source={{ uri: this.props.navigation.state.params.uri }}/>
</View>
Upvotes: 2
Views: 330
Reputation: 3035
Could this work?
<View style={{
width: '100%',
aspectRatio={4.0/5.0}
}}>
<Image
style={{
width: '100%',
height: '100%',
}}
resizeMode={'contain'}
source={...}/>
</View>
Upvotes: 1