Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Image 'contain' resizeMode not working in react native

I am using React Native on a real Android device. When creating a really simple app with just the following render function on the main app component...

render() {   <Image     source={{uri:'http://resizing.flixster.com/DeLpPTAwX3O2LszOpeaMHjbzuAw=/53x77/dkpu1ddg7pbsk.cloudfront.net/movie/11/16/47/11164719_ori.jpg'}}     style={       {         flex: 1,         resizeMode: 'contain',         backgroundColor: 'yellow'       }               } /> } 

I get the following result on my device: screenshot_2016-04-05-11-05-41

As you can see the whole background is yellow so that tells us the image element is taking the whole screen size indeed. But it is just rendered wrong. The 'cover' resizeMode does work as expected (and so does the 'stretch' mode). It is the 'contain' mode that is not working (the most important one from my point of view). The problem gets even worse when placing the image on a ListView since the image does not even show.

UPDATE 1 As Frederick points out, 'contain' only works when the image is larger than the container size. So how can we make the image take the whole container size while keeping its aspect ratio? Percentages are not supported yet by styles in React, and I don't know how to get the image width and height properties once the image is loaded. None of the events associated with the Image component provide that info.

UPDATE 2 Good news. I am now using React Native v0.24.1 and it seems the image 'contain' mode now works as expected, even when the actual image size is smaller than its container. zvona's solution is good (although you need to bear in mind that onLayout will give you the image view size the image is rendered in, but NOT the actual image size being loaded). As for now, I don't know of any way to find out the actual image size (let's suppose you are retrieving the image from a network resource and you don't know the size, which could be very important if you want to calculate its aspect ratio).

like image 354
user3621841 Avatar asked Apr 05 '16 20:04

user3621841


People also ask

How do I make an image fit 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 do I display a PNG image in react native?

Adding Image Let us create a new folder img inside the src folder. We will add our image (myImage. png) inside this folder. We will show images on the home screen.

What is aspect ratio in react native?

aspectRatio ​ Aspect ratio controls the size of the undefined dimension of a node. See https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio for more details. On a node with a set width/height, aspect ratio controls the size of the unset dimension.


1 Answers

This is the trick:

 render() {     return (         <Image           style={{ flex: 1, height: undefined, width: undefined }}           source={require("../../resource/image/bg_splash.jpg")}           resizeMode="contain"         />      );   } 
like image 152
Rahul Rastogi Avatar answered Sep 21 '22 06:09

Rahul Rastogi