Understanding “resizeMode” in React Native
There are different options in Image “reiszeMode” (‘cover’, ‘contain’, ‘stretch’, ‘repeat’, ‘center’). It can be tricky which one to use if you does not have understand it properly. Let Me Explain to you in detail by understanding the basics.
Basic Concepts
1 ) Aspect Ratio : The aspect ratio of an image describes the proportional relationship between its width and its height. Two numbers separated by a colon like 3:2 (width:height) . Square has 1:1 aspect ratio
Note : Thus, aspect ratio concerns the relationship of the width to the height, not an image’s actual size.
2 )Scale the image uniformly (maintain the image’s aspect ratio) : When you scale a image it is better to manage the aspect ratio otherwise the image will be stretched and it will look bad
Some Examples :
- On left side scale image size 400*400(1:1) and it maintain actual aspect ratio(1:1) and looks perfect
- on right scale image size is 400*200(2:1) and it does not maintain actual aspect ratio(1:1) and looks bad
3 ) Dimension of Image View In React Native : When you have to show image from server in react native you have to define the width and height of image . Both are called the dimension of image view
Now the dimension of image view will be 450*300
Now Back the to the topic “reiszeMode” after understanding the basics
reiszeMode Options
1 ) “cover” :
After Scaling with aspect ratio this will happen
Scale Image Width ≥ Image View Dimension Width
Scale Image Height ≥ Image View Dimension Height
Let me explain you in simple words with an example :
Image size = “1200*1200” (aspect ratio 1:1) , Image View Dimension “300*200”, and resizeMode = “cover” , when the image is scaled this will happen
- Take Maximum value from Dimensions. It can be width or height . For “300*200” it will be “300” . [max value = 300(width) ]
- Width has been resized to 300 so height will also be resized to “300” because image has aspect ratio 1:1 and scaling is done by maintaining the aspect ratio
Result :
- Scale image size “300*300”
- Image View Dimension “300*200”
- As you can see in picture
- it is showing full width but height is cropped from center and not showing full height
Some More Examples :
Note : If you want to show full width and height, Scale Image aspect ratio must match image view dimension aspect ratio
Scale Image Size : 350*350 (1:1 ratio)
Image View Dimension : 350*350 (1:1 ratio)
Result : Showing full image
2 ) “contain” :
After Scaling with aspect ratio this will happen
Scale Image Width ≤ Image View Dimension Width
Scale Image Height ≤ Image View Dimension Height
Let me explain you in simple words with an example :
Image size = “1200*1200” (aspect ratio 1:1) , Image View Dimension “300*200”, and resizeMode = “contain” , when the image is scaled this will happen
- Take Minimum value from Dimensions. It can be width or height . For “300*200” it will be “200” . [min value = 200(height) ]
- Height has been resized to 200 so width will also be resized to “200” because image has aspect ratio 1:1 and scaling is done by maintaining the aspect ratio
Result :
- Scale image size “200*200”
- Image View Dimension is “300*200”
- As you can see in picture
- it will show full height but will show empty space from left and right in width.
Some More Examples :
3 ) “stretch” :
Scale width and height independently, This may change the aspect ratio of the src.
it will not maintain any aspect ratio . it will scale image to dimensions of image view without maintaining the aspect ratio.
After Scaling this will happen
Scale Image Width = Image View Dimension Width
Scale Image Height = Image View Dimension Height
- Original Image : 1200*1200
- Image Dimension : 300*200
- Scale Image Size : 300*200
- Result : As it is square image and ratio should be 1:1 but it is 3:2 so image will be stretched
- It does not maintain the actual aspect ratio when scaling, it change the ratio from 1:1 to 3:2 due to image dimension and as result image is stretched
- You can see in picture image is stretched
4 ) “repeat” :
As the name suggest it will repeat the image in frame of view
Frame Size is 300*200 and image in repeating in that frame
5) “center” :
It will show the image in center of frame . if image size is larger than it will scale it down uniformly
Showing image in center of frame as you can in above picture
Thanks for reading my article . If you have enjoyed this article, feel free to hit that clap button 👏 to help others find it.