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?
UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .
Here is the essential problem: the only way in which UIImageView interacts with Auto Layout is via its intrinsicContentSize property. That property provides the intrinsic size of the image itself, and nothing more.
Here is a simple way: UIImage * image = [UIImage imageNamed:@"image"]; CGSize sacleSize = CGSizeMake(10, 10); UIGraphicsBeginImageContextWithOptions(sacleSize, NO, 0.0); [image drawInRect:CGRectMake(0, 0, sacleSize.
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.
Setting clipsToBounds in combination with UIViewContentModeScaleAspectFit contentMode was what did the trick for me. Hope that helps someone!
imageView.clipsToBounds = YES; imageView.contentMode = UIViewContentModeScaleAspectFit;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With