Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fit image to screen

Tags:

react-native

I'm trying to simply display an image and have it autoscale to fit the screen. According to the docs resizeMode: 'contain' should do what I want. However, nothing I've tried seems to work. I've tried manually setting the width and height. I've tried setting them to undefined. I've tried using Dimensions.get("window").height but no matter what the image is not resized and it runs off the bottom and right side of the screen.

Here is the original image: Original image

Here is how it gets rendered:

enter image description here

Here is my code:

<View>
   <Image source={require('../../images/wireframe-car.jpg')} style={{resizeMode: 'contain'}} />
</View>

What am I missing?

Thanks in advance

like image 229
kent Avatar asked Dec 14 '22 14:12

kent


1 Answers

I think you can achieve by add more maxWidth and maxHeight

 const {width, height} = Dimensions.get("window")
 render() {
   return(
    <View>
      <Image source={require('../../images/wireframe-car.jpg')}
        resizeMode='contain'
        style={{
          maxHeight: height,
          maxWidth: width
          }} />
    </View>
   )
 }
like image 82
Tung Duong Avatar answered Apr 05 '23 07:04

Tung Duong