Full width image using flexbox?
I'm attempting to make an image full-width and came across a few stackoverflow questions related where the top upvoted answer was to use flexbox similarly to how i'm using it below. Although it doesn't seem to display the image unless i set static widths on my styles property. Any ideas?
var styles = StyleSheet.create({
canvasContainer: {
flex:1,
alignItems: 'stretch'
},
canvas: {
flex:1
// width: 200,
// height: 200
}
});
render: function() {
return (
<View style={styles.container}>
<View style={styles.canvasContainer}>
<Image
source={{uri: 'http://imgs.steps.dragoart.com/how-to-draw-a-pony-step-7_1_000000053055_5.jpg'}}
style={styles.canvas} />
</View>
<ScrollView
style={styles.scrollView}
scrollEventThrottle={200}
contentInset={{top: 0}}
>
{this.state.messages.map(m => {
return <Text style={styles.message}>Josh: {m}</Text>
})}
</ScrollView>
</View>
)
}
Posting this here in case anyone gets in the same trouble as i have. I wanted to be able to put an image in a container and have it fill to that container. The following seems to work so far in any situation. The view wrapping the image with flexDirection of row is what it all hinges on which may be me just being bad still getting used to flex positioning.
<View
style={{flex:1,flexDirection: 'row'}}>
<Image
resizeMode='contain'
style={{
flex:1,
width:null,
height:null}}
source={Images[status]}
/>
</View>
@jwtea Thank you! Your variant works for me, but I changed resize mode to 'cover' and now it works perfect!
Getting <Image> width:100% and height auto is so finicky. Trying on Android right now. Always such a battle.
What I do now is have an image component that based on the width/height of the image it sets the flex properties and such so that I can achieve width 100% without changing the aspect ratio and without the image overflowing its container. The source for my images are set with require('...') so Image.getSize() doesn't work on them but the following does and works synchronously:
const resolveAssetSource = require('resolveAssetSource')
let { width, height } = resolveAssetSource(imageSource)
@jwtea solution is working with me like a charm 🎉 known that I am using flex containers
I gave up and had to create an Image component with onLoad to get the source image/width and then setState to set the image dimensions. I will revisit later, this is too much overhead for something so simple.
@Noitidart Totally agreed. The team made the decision to not mimic the concept of automatically sizing images and maintaining their aspect ratio as it is on the web. I can see the pros for that but boy it sure is a pain to get an image to be width 100% (yet not wider than it's container) while having the height automatically adjust. It's such a common use-case that it would be nice to have something for this out of the box someday
For me nothing worked except this approach: https://stackoverflow.com/a/42556256 Other solutions depend top container configurations / styling
I agree with @Noitidart. We've been dealing with image tags since the birth of WWW. Why is it still so difficult and wonky requiring dozens of kludgy workarounds? I've tried nearly every suggestion here. So far, none of them have worked. I'm just venting. So feel free to ignore.
How should I proceed, If I have to show full width and auto height of images inside a list (flatlist/sectionlist/listview) like the Instagram app?
The current Examples work well as in Individual Component only, When I put the component inside renderItem of flatlist the images don't show.
<FlatList data={['a', 'b', 'c']}
renderItem={({ item }) => (
<View>
<Text>{item}</Text>
<Image
style={{ flex:1, width: null, height: null } }
resizeMode="contain"
source={{ uri: 'http://www.transphone.net/images/android-transfer/android-screenshot2.jpg'}} />
</View> )}
/>
Any Help is much Appreciated.
Here's a module I made that does exactly this https://www.npmjs.com/package/react-native-fullwidth-image
I found that @jwtea 's solution almost worked for me (thanks Jack!), but although the image was scaled horizontally, there was excessive automatic padding (whitespace) inserted above and below the image. Adding an aspectRatio with correct values for my image solved this:
<View
style={{flex: 1, flexDirection: 'row'}}>
<Image
resizeMode='contain'
style={{
flex: 1,
width: null,
height: null,
aspectRatio: 926/606}}
source={Images[status]}
/>
</View>
In my case my image was 926 wide and 606 high, so I just used those values directly. A more sensible developer might have simplified the aspect ratio of their source image first to e.g. 16/9!
For me, this works across both Android and iOS, tested on a few device sizes.
Here is my solution;
const img = (source) => {
const { width, height } = Image.resolveAssetSource(source)
return <Image source={source} style={{ width: null, height: null, resizeMode: 'cover', aspectRatio: width / height }} />
}
<View style={{ flexDirection: 'row' }}>
<View style={{ flex:2 }}>
{img(require('./assets/images/1.png'))}
</View>
<View style={{ flex:1 }}>
{img(require('./assets/images/2.png'))}
</View>
</View>