Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Imageview always stretched show image stretched iOS?

I am using an imageView and it contains different images, but some images are stretched. How do I display the images without any stretch? I have set the contentMode to aspect fit. However, the images are not in the size of the imageView, and it is in different sizes. I want to show the images as the same size without stretching.

   imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)];
   imageview1.contentMode = UIViewContentModeScaleAspectFit;
   imageview1.clipsToBounds = YES;
like image 549
ashmi123 Avatar asked Mar 11 '23 06:03

ashmi123


2 Answers

Please try setting content mode before setting frame like below

 imageview1.contentMode = UIViewContentModeScaleAspectFit;
 imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)];      
 imageview1.clipsToBounds = YES;
like image 116
Abu Ul Hassan Avatar answered Mar 21 '23 02:03

Abu Ul Hassan


If you want to display each image without stretching and of same size, than set contentMode property of UIImageView's instance to UIViewContentModeScaleAspectFill. For instance-

imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)];
imageview1.contentMode = UIViewContentModeScaleAspectFill;
imageview1.clipsToBounds = YES;

UIViewContentModeScaleAspectFill, will fill the entire area of image view, keeping the aspect ratio intact. In the process of filling entire image view, either vertical or horizontal length will be fully covered and the filling will continue till the time other dimension is fully filled. In the process, your content(either across vertical or horizontal) will be visible outside the frame. To clip this extra content we have set clipsToBounds property of image view to YES.

UIViewContentModeScaleAspectFit, will fill the image view's area till the time any one length either vertical or horizontal is fully filled keeping the aspect ratio intact. Its useful, if you are not required to show each image as same size as the other direction (if vertical is fully filled than horizontal or vice versa), is not fully covered. Since this will show blank spaces in the direction which is not fully covered.

like image 23
Sanjay Mohnani Avatar answered Mar 21 '23 03:03

Sanjay Mohnani