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?
Update for Swift 3:
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.
Setting clipsToBounds in combination with UIViewContentModeScaleAspectFit contentMode was what did the trick for me. Hope that helps someone!
Use this for your
UIImageView
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:
There will be some empty space, but scale is preserved.