ashmi123
ashmi123

Reputation: 710

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;

Upvotes: 4

Views: 1033

Answers (2)

Sanjay Mohnani
Sanjay Mohnani

Reputation: 5967

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.

Upvotes: 3

Abu Ul Hassan
Abu Ul Hassan

Reputation: 1396

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;

Upvotes: 4

Related Questions