Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UIImageView mode iPhone/iPad

Tags:

If I have an UIImageView with the mode set to center for example how can I change it to something else such as aspect fit with code?

enter image description here

like image 243
Tono Nam Avatar asked Jul 20 '11 20:07

Tono Nam


People also ask

What is the difference between a UIImage and a UIImageView?

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

What is a UIImage?

An object that manages image data in your app.

How do I add an image to UIImageView?

Since you have your bgImage assigned and linked as an IBOutlet, there is no need to initialize it as a UIImageView... instead all you need to do is set the image property like bgImage. image = UIImage(named: "afternoon") . After running this code, the image appeared fine since it was already assigned using the outlet.


2 Answers

imageView.contentMode = UIViewContentModeScaleAspectFit; 
like image 178
jamapag Avatar answered Sep 23 '22 01:09

jamapag


yourImageView.contentMode = UIViewContentModeScaleAspectFit; 

you can set it to any of the following

UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, UIViewContentModeScaleAspectFill, UIViewContentModeRedraw, UIViewContentModeCenter, UIViewContentModeTop, UIViewContentModeBottom, UIViewContentModeLeft, UIViewContentModeRight, UIViewContentModeTopLeft, UIViewContentModeTopRight, UIViewContentModeBottomLeft, UIViewContentModeBottomRight 
like image 38
Sascha Avatar answered Sep 23 '22 01:09

Sascha