How to stop image from stretching in UIImageView?

I have a UIImageView where I set the frame size x = 0, y = 0, width = 404, height = 712 . In my project, I need to change the image in UIImageView dynamically.

I use this code to change the image:

 self.imageView.image = [UIImage imageNamed:@"setting-image.png"]; 

The problem is that the *.png size is smaller than the UIImageView frame UIImageView , the image is stretched. I do not want him to stretch. Is there any way to do this?

+64
ios cocoa-touch uiimage uiimageview
Aug 12 '11 at 4:15
source share
10 answers

You can use

 self.imageView.contentMode = UIViewContentModeScaleAspectFit; 

Swift 3:

 imageView.contentMode = .scaleAspectFit 

Either UIViewContentModeCenter / .center , or any other mode described in the UIView documentation .

+132
Aug 12 2018-11-11T00:
source share

Setting clips in conjunction with the UIViewContentModeScaleAspectFit contentMode was a trick for me. Hope someone helps!

 imageView.clipsToBounds = YES; imageView.contentMode = UIViewContentModeScaleAspectFit; 
+11
Nov 21 '14 at 15:49
source share

Use the contentMode property. You probably want either a UIViewContentModeScaleAspectFit or a UIViewContentModeCenter .

+5
Aug 12 2018-11-11T00:
source share

Change the UIImage mode from "scale to fill" to "Center" in the interface builder. Make sure your image is the exact size you need.

enter image description here

+4
Jul 07 '13 at 14:25
source share

You must set CGSize as the width and height of the image so that the image does not stretch and it will be located in the middle of the image.

 - (UIImage *)imageWithImage:(UIImage *)image scaledToFillSize:(CGSize)size { CGFloat scale = MAX(size.width/image.size.width, size.height/image.size.height); CGFloat width = image.size.width * scale; CGFloat height = image.size.height * scale; CGRect imageRect = CGRectMake((size.width - width)/2.0f, (size.height - height)/2.0f, width, height); UIGraphicsBeginImageContextWithOptions(size, NO, 0); [image drawInRect:imageRect]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } 
+4
Aug 11 '16 at 9:36 on
source share

Update for Swift 3:

 imageView.contentMode = .scaleAspectFit 
+3
Nov 25 '16 at 20:41
source share

Change the contentMode, for example:

 imageView.contentMode = UIViewContentModeScaleAspectFit; 
+2
Aug 12 '11 at 4:20
source share

How to use the original image size - without stretching

  • Just change the UIImage mode from scale to fill to Aspect Fit inside the interface builder.

enter image description here

+2
Jun 24 '15 at 10:45
source share

Use this for UIImageView

 imageView.contentMode = UIViewContentModeScaleAspectFill; 

You will not get any space and keep the scale. However, part of the image will be clipped.

If you use the following:

 imageView.contentMode = UIViewContentModeScaleAspectFit; 

There will be empty space, but the scale is maintained.

+1
May 6 '16 at
source share

still stretched when the image is larger than the image

fast

  let qCodeImage = UIImage(named: "qcode-placeholder.png")! let qCodeImageView = UIImageView(frame: CGRectMake(0, 0, CGRectGetWidth(cardView.frame)-30, CGRectGetWidth(cardView.frame)-30)) qCodeImageView.image = qCodeImage qCodeImageView.clipsToBounds = true qCodeImageView.contentMode = UIViewContentMode.ScaleToFill qCodeImageView.center = cardView.center 
0
Sep 10 '15 at 9:01
source share



All Articles