Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set image width to be 100% and height to be auto in react native?

I am trying to display list of images in a scrollview. Width should be 100%, while height should be automatic, keeping aspect ratio.

The searches I did pointed to various solutions which give fullscreen background style.

const styles = StyleSheet.create({     image: {         width: null,         height: 300,         resizeMode: 'cover'     } });  <ScrollView style={{flex: 1}}>     <Image style={styles.image} source={require('../../../images/collection-imag1.png')}/>     <Image style={styles.image} source={require('../../../images/collection-imag2.png')}/> </ScrollView> 

I have tried various combinations of width: null, height: null, flex: 1, alignSelf etc. The above solution is almost working, except the height is not dynamic. Parts of the image are not visible.

like image 722
Prabhakar Bhat Avatar asked Sep 22 '16 06:09

Prabhakar Bhat


People also ask

How do I set the width and height of an image in React Native?

So all we need to do is create a View with width: "100%" , then use onLayout to get the width of that view (i.e. the container width), then use that container width to calculate the height of our image appropriately.

How do you set dynamic height and width in React Native?

In react-native, to set the dynamic width and height to a component, we use built-in interface Dimensions. it takes the dimension of a given component and then sets the width and height to its equivalent.

How do I give a full width image in React Native?

To fit an Image to the full width of the screen and maintain aspect ratio with React Native, we can set the width to null and the height to the height we want. to set the height of the Image to 300 and the width to null to fit the Image to make the width flexible and maintain aspect ratio.

How to get the size of a ScrollView image in React Native?

You can get the size using Image.getSize static method from react-native You always have to set the width and height of an Image. It is not going to automatically size things for you. The React Native docs says so. You should measure the total height of the ScrollView using onLayout and set the height of the Image s based on it.

How to add height and width on an image in React Native?

You should always add a height and width on an image in React Native. In case the image is always the same, you can use Dimensions.get ('window').width to calculate the size the image should be. For example, if the ratio is always 16x9, the height is 9/16th of the width of the image. The width equals device width, so:

How to set image width to be 100% and height to be auto?

To set image width to be 100% and height to be auto in React Native, we can set the width and height of the Image. to call Dimensions.get with 'window' to get the window’s dimension. Then we calculate the ratio between the width and height of the image with win.width / 200, where 200 is the width of the image.

How to work with remote images in React Native?

Always remember, in react native, there is no way to work with remote images without providing height, width values. You need to know these values. There is also a way by which you can work with only one value of either width or height. In that case you will need the value of aspect ratio.


2 Answers

So after thinking for a while I was able to achieve height: auto in react-native image. You need to know the dimensions of your image for this hack to work. Just open your image in any image viewer and you will get the dimensions of the your image in file information. For reference the size of image I used is 541 x 362

First import Dimensions from react-native

import { Dimensions } from 'react-native'; 

then you have to get the dimensions of the window

const win = Dimensions.get('window'); 

Now calculate ratio as

const ratio = win.width/541; //541 is actual image width 

now the add style to your image as

imageStyle: {     width: win.width,     height: 362 * ratio, //362 is actual height of image } 
like image 157
Vinit Kadam Avatar answered Nov 03 '22 00:11

Vinit Kadam


"resizeMode" isn't style property. Should move to Image component's Props like below code.

const win = Dimensions.get('window');  const styles = StyleSheet.create({     image: {         flex: 1,         alignSelf: 'stretch',         width: win.width,         height: win.height,     } });  ...     <Image         style={styles.image}        resizeMode={'contain'}   /* <= changed  */        source={require('../../../images/collection-imag2.png')} />  ... 

Image's height won't become automatically because Image component is required both width and height in style props. So you can calculate by using getSize() method for remote images like this answer and you can also calculate image ratio for static images like this answer.

There are a lot of useful open source libraries -

  • react-native-auto-height-image
  • react-native-fullwidth-image
  • react-native-fit-image
like image 38
lwinkyawmyat Avatar answered Nov 03 '22 01:11

lwinkyawmyat