Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Image resizeMode: cover with bottom positioning

hello guys I have a problem with Image positioning. I would like to achieve to position my Image like resizeMode="cover" + background-position: "bottom". So if Image overflows, i need to let the image be drew from bottom of the screen and "cropped" from sides and top. Is something like this even possible? My current code is:

<View style={{
  height: ILLUSTRATION_HEIGHT,
  width: ILLUSTRATION_WIDTH,
  position: "relative",
  overflow: "hidden"
}}>
    <Image
      width={ILLUSTRATION_WIDTH}
      height={ILLUSTRATION_HEIGHT}
      resizeMode="cover"
      source={{ uri: "illustration" }}
      style={{
        position: "absolute",
        bottom: 0,
        width: "100%",
        height: "100%"
        }}
      />
</View>

Maybe I did not describe it well enough so here is a picture of what I'm trying to achieve:

What I want to achieve

Note: Dashed part is the part of Image, that is actually showed from full picture.

Thank you so much!

like image 407
sbqq Avatar asked Apr 29 '19 14:04

sbqq


People also ask

How do you place image at the bottom of the screen in react-native?

The solution is to use add a container for the button, and use flex-end inside so that the button moves to the bottom.

How do I stretch an image in react-native?

resizeMode ​ stretch : Scale width and height independently, This may change the aspect ratio of the src. repeat : Repeat the image to cover the frame of the view.

How do I align an image to the left in react-native?

Regarding the alignment of the image, react-native doesn't really care. Images are placed at the middle of the container. If you want to place the image left-aligned, you'll have to write a small algorithm which compares the actual dimensions of the image and dimensions of the container.

How do I change 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.


1 Answers

By using width and height '100%' you can't get exactly what you wanted to, because the image will fill all the available space, I suggest debugging your UI using backgroundColor or ctrl+D and 'toggle inspector' then check what exactly happens in your view. This is a snippet of code that responds to your need (right image 'WHAT I NEED):

<View
        style={{
          height: 150,
          width: 150,
          position: 'relative',
          overflow: 'hidden',
          backgroundColor:'red',
          alignItems:'center'
        }}>
        <Image
        
          resizeMode="cover"
          source={{ uri: 'https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500' }}
          style={{
            position: 'absolute',
            bottom: 0,
            width: '90%',
            height: '90%',
          }}
        />
      </View>

you will get something like that : Result image

like image 160
ikerfah Avatar answered Nov 15 '22 03:11

ikerfah