4

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;
Cœur
  • 37,241
  • 25
  • 195
  • 267
ashmi123
  • 710
  • 1
  • 6
  • 21
  • UIViewContentModeScaleAspectFill – Himanshu Moradiya Jan 03 '17 at 09:41
  • try by changing clipsToBounds = false; – Vishal Sonawane Jan 03 '17 at 09:41
  • Yes I use that also but it will not take whole image some of portion will cut using "UIViewContentModeScaleAspectFill". – ashmi123 Jan 03 '17 at 09:42
  • @VishalSonawane Still its not working do u have any idea about any third party library? – ashmi123 Jan 03 '17 at 09:43
  • @ashmi123 what is the size of your image you set in imageview ? – Himanshu Moradiya Jan 03 '17 at 09:44
  • `UIViewContentModeScaleAspectFit ` should not result stretched, do you have anything else to do with the view? – Tj3n Jan 03 '17 at 09:45
  • *ImageView take size of images*. This is incorrect, you are using aspect fit, so image will always fill the `imageView` either using complete width **or** complete height unless the image is perfectly square. If for example, you are using a landscape image, then width will be longer than height and while maintaining the aspect ratio, the height of the image **inside** the `UIImageView` will be decided and the image will be appropriately resized, and it will **not** take up the full height. The `UIImageView` **will** have the frame you set it to. The `UIImage` **may not** take up the full bound – Rikh Jan 03 '17 at 09:45
  • @Tj3n you are correct but it will change the imageview according to image size that i dont want ,i want fix Imageview size. – ashmi123 Jan 03 '17 at 09:46
  • @ashmi123 Sorry I don't hav any Idea about such library. I think the problem is occurring due to wrong frame of image view. Have a look at this question to get more clear idea. http://stackoverflow.com/questions/16878607/change-uiimageview-size-to-match-image-with-autolayout – Vishal Sonawane Jan 03 '17 at 09:47
  • @Rikh Yes but here this happen when I set any UIImage to image view using the aspectFit property it will change the size of imageview – ashmi123 Jan 03 '17 at 09:48
  • try it with Aspect Fill and just take a screen that show image stretched – Himanshu Moradiya Jan 03 '17 at 09:53
  • Aspect fill not stretched it will cut the size of image which is out of the imageview. – ashmi123 Jan 03 '17 at 09:54
  • Again, `UIImageView` size is **not** changing, it appears like that because you are using aspect fit and different images have different aspect ratios. You can use `scaleToFill` but then the image will appear weirdly distorted. `Aspect fit` will make different images appear as if the `imageView` is resized. As Himanshu suggested, you can use `aspectFill` with `clipsToBounds` enabled but this **will** cause clipping. Unless all your images are of the same size, this **will** happen. – Rikh Jan 03 '17 at 09:55
  • So is there any solution on above how other apps like tinder and instagram shows image with good quality still will upload stretched Pictures? If have please suggest me. – ashmi123 Jan 03 '17 at 09:57
  • imageview1 = [[UIImageView alloc]initWithFrame:CGRectMake(30, 10, 190, 190)]; why you user give fix hight width ? – Himanshu Moradiya Jan 03 '17 at 10:06
  • refer this http://stackoverflow.com/a/14650811/4637057 – Vincent Joy Jan 03 '17 at 10:09
  • Thanks to all for helping....Thank u... – ashmi123 Jan 03 '17 at 10:14

2 Answers2

4

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;
Abu Ul Hassan
  • 1,340
  • 11
  • 28
3

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.

Sanjay Mohnani
  • 5,947
  • 30
  • 46