71

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

I am using this code to change the image:

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

The problem is, when the *.png image size is smaller than the UIImageView frame size, the image stretches. I don't want it to stretch. Is there any way to do that?

Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
R. Dewi
  • 4,141
  • 9
  • 41
  • 66

10 Answers10

142

You can use

self.imageView.contentMode = UIViewContentModeScaleAspectFit;

Swift 3:

imageView.contentMode = .scaleAspectFit

Or UIViewContentModeCenter / .center, or any of the other modes described in the UIView documentation.

Cœur
  • 37,241
  • 25
  • 195
  • 267
jtbandes
  • 115,675
  • 35
  • 233
  • 266
  • thank you for your answer, but using mode aspect fit make some image's position too below, so i change it into top left mode, thank you for suggesting me to using it, finally, i've got the answer :) – R. Dewi Aug 12 '11 at 04:53
12

Setting clipsToBounds in combination with UIViewContentModeScaleAspectFit contentMode was what did the trick for me. Hope that helps someone!

imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;
Tyler Kidd
  • 121
  • 1
  • 4
6

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

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
Anomie
  • 92,546
  • 13
  • 126
  • 145
4

Change the UIImage's Mode from 'scale to fill' to 'Center' within the interface builder. Make sure your image is the exact size you need.

enter image description here

Declan McKenna
  • 4,321
  • 6
  • 54
  • 72
4

You have to set CGSize as your image width and hight so image will not stretch and it will arrange at the middle of imageview.

- (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;
}
Kaushik Movaliya
  • 799
  • 11
  • 27
3

Update for Swift 3:

imageView.contentMode = .scaleAspectFit
transistor
  • 649
  • 6
  • 11
2

How to use original size of image - without any stretching

  • Just change the UIImage's Mode from scale to fill to Aspect Fit within the interface builder.

enter image description here

Megaetron
  • 1,154
  • 2
  • 15
  • 29
2

Change the contentMode, e.g.:

imageView.contentMode = UIViewContentModeScaleAspectFit;
Nathanial Woolls
  • 5,231
  • 24
  • 32
1

Use this for your UIImageView

imageView.contentMode = UIViewContentModeScaleAspectFill;

You won't get any space and with scale preserved. However, some part of the image will be clipped off.

If you use the following:

imageView.contentMode = UIViewContentModeScaleAspectFit;

There will be some empty space, but scale is preserved.

jherran
  • 3,337
  • 8
  • 37
  • 54
0

still stretch when image is bigger than imageivew

swift

    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
Hiren
  • 12,720
  • 7
  • 52
  • 72