Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clip off the image if its outside bounds react native

I'm new to react native development. I want the image to be in aspect ratio and when I try to change its bounds the area outside the bounds should be clipped off. In Android, it is working correctly but not in iOS the image is not clipping to its bounds and showing the whole image.

<View
      style={{
        backgroundColor: "#04894a",
        padding: 10,
        height: 110,
        alignItems: "center"
      }}
    >
 <View
        style={{
          alignItems: "center"
        }}
      >
        <Image
          style={{ height: 150, width: 150 }}
          source={require("../images/wow_logo.png")}
        />
      </View>

The image is going outside the view but still, it is showing in full. I want it to be cropped if going outside the bounds. Can anyone help me with this? Any links or suggestions are also appreciated. TIA

like image 532
Arun K Avatar asked Jul 21 '18 10:07

Arun K


People also ask

How do I make an image fit in react-native?

How do you change the size of image in React Native? To resize image with React Native, we set the width and height of the image to percentages and set resizeMode to 'cover' .

How do you overlap view in react-native?

You have have to use position:'absolute' and put the circle element as the last element of the elements list so it comes to top (no need to use zIndex). also the container div must have styles for child elements to be centered. There won't be an issue since you can position that container div where ever you want.

How do I add an external image in react-native?

To load images in react native from URL you must specify the image width, and height. React native cannot determine the width and height from downloaded images. After specifying width, and height we can specify the URL to the image source prop. Inside this prop we will specify a uri which holds our image URL.


1 Answers

Use overflow: hidden - by default in react native the overflow flag is visible.

Also to maintain the aspect ratio, just have one parameter width or height to the image, then add style={{ aspectRatio: 3/2 }} so that the width:height ratio is maintained.

like image 90
awaleed Avatar answered Oct 22 '22 18:10

awaleed