How to control content mode of UIImageView?

I have a UIImageView with some fixed rectangle size. But my images are not fixed in size. I want to display images according to the size of the UIImageView rect. Whether the image is large or large in resolution, it should be in a fixed form in accordance with the size of the UIImageView. I am confused about using below assets.

UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, UIViewContentModeScaleAspectFill, UIViewContentModeRedraw 

What needs to be set in the auto-size mask? How do they behave?

+44
ios iphone ipad uiimageview
Jan 03 '13 at 6:26
source share
7 answers

Please try this code, hope it works for you.

set the UIImageView contentMode to UIViewContentModeScaleAspectFill , as shown below:

 imageView.contentMode = UIViewContentModeScaleAspectFill; 

install autoresizingMask from UIImageView as shown below:

 imageView.autoresizingMask = ( UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleWidth ); 
+84
Jan 03 '13 at 6:56
source share

Here's a quick solution:

 let imageView = UIImageView(image: UIImage(named: "backgroundImage")) imageView.frame = tableView.frame imageView.contentMode = .ScaleAspectFill tableView.backgroundView = imageView 
+2
May 13 '15 at 9:33
source share

If you want to make your UIImageView size in accordance with the ratio, then you must set it to UIViewContentModeScaleAspectFit

  yourimage.contentMode=UIViewContentModeScaleAspectFit; 

let me know if it works or not !!!

Happy coding.

+1
Jan 03 '13 at 6:29
source share

In fast mode, you also set the content mode

 var newImgThumb : UIImageView newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70)) newImgThumb.contentMode = .ScaleAspectFit 
+1
Nov 14 '14 at 5:56
source share

It is worth noting that swift 3 has changed as follows

. ScaleAspectFit to ScaleAspectFit

. ScaleAspectFill to. ScaleAspectFill

+1
Oct 23 '16 at 0:15
source share

Hope this helps you.

 CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect); image = [UIImage imageWithCGImage:imageRef]]; CGImageRelease(imageRef); 
0
Jan 03 '13 at 6:50
source share

You just need these two lines:

 imageView.contentMode = UIViewContentModeScaleAspectFill; imageView.layer.clipsToBounds = YES; // Must do this or else image will overflow 
0
Jul 27 '17 at 6:16
source share



All Articles