How to stop an image from stretching within a UIImageView?

IosCocoa TouchUiimageviewUiimageStretching

Ios Problem Overview


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?

Ios Solutions


Solution 1 - Ios

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.

Solution 2 - Ios

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

imageView.clipsToBounds = YES;
imageView.contentMode = UIViewContentModeScaleAspectFit;

Solution 3 - Ios

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

Solution 4 - Ios

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

Solution 5 - Ios

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;
}

Solution 6 - Ios

Update for Swift 3:

imageView.contentMode = .scaleAspectFit

Solution 7 - Ios

Change the contentMode, e.g.:

imageView.contentMode = UIViewContentModeScaleAspectFit;

Solution 8 - Ios

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

Solution 9 - Ios

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.

Solution 10 - Ios

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionR. DewiView Question on Stackoverflow
Solution 1 - IosjtbandesView Answer on Stackoverflow
Solution 2 - IosTyler KiddView Answer on Stackoverflow
Solution 3 - IosAnomieView Answer on Stackoverflow
Solution 4 - IosDeclan McKennaView Answer on Stackoverflow
Solution 5 - IosKaushik MovaliyaView Answer on Stackoverflow
Solution 6 - IostransistorView Answer on Stackoverflow
Solution 7 - IosNathanial WoollsView Answer on Stackoverflow
Solution 8 - IosMegaetronView Answer on Stackoverflow
Solution 9 - IosBaraiya BhadreshView Answer on Stackoverflow
Solution 10 - IosDaniel LongView Answer on Stackoverflow