Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set contentMode of UIImageView

Tags:

ios

swift

In Obj-C

imageView.contentMode = UIViewContentModeScaleAspectFill;

would set the contentMode.

Why does

imageView.contentMode = UIViewContentModeScaleAspectFill

not work in Swift?

like image 705
Ríomhaire Avatar asked Jun 08 '14 17:06

Ríomhaire


People also ask

What is the difference between UIImage and UIImageView?

UIImage contains the data for an image. UIImageView is a custom view meant to display the UIImage .

What is use of UIImageView?

Overview. Image views let you efficiently draw any image that can be specified using a UIImage object. For example, you can use the UIImageView class to display the contents of many standard image files, such as JPEG and PNG files.

How do I install UIImageView?

First you create a UIImage from your image file, then create a UIImageView from that: let imageName = "yourImage. png" let image = UIImage(named: imageName) let imageView = UIImageView(image: image!)

What is content mode?

The content mode specifies how the cached bitmap of the view's layer is adjusted when the view's bounds change. This property is often used to implement resizable controls.


2 Answers

Somewhat confusingly, Swift drops the prefix for ObjC enum values:

imageView.contentMode = .scaleAspectFill 

This is because Swift already knows what enum type is being used. Alternatively, you can specify the enum too:

imageView.contentMode = UIViewContentMode.scaleAspectFill 

Note: In versions of Swift before version 3, "scaleAspectFill" will need to be capitalized.

like image 61
BergQuester Avatar answered Sep 27 '22 19:09

BergQuester


In swift language we can set content mode of UIImage view like

var newImgThumb : UIImageView newImgThumb = UIImageView(frame:CGRectMake(0, 0, 100, 70)) newImgThumb.contentMode = .ScaleAspectFit  
like image 22
Ali Raza Avatar answered Sep 27 '22 19:09

Ali Raza