Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Checking size of UIImageView

I am trying to print a line to the debugger/console to show the dimensions of the UIImageView object I have. I was trying to use the following code but get an error saying UIImageView doesn't have a member named 'size'. This was my code:

    @IBOutlet var Image: UIImageView!

override func viewDidLoad() {

        super.viewDidLoad()


    var imageHeight = Image.size.height
    var imageWidth = Image.size.width

print (imageHeight)
print (imageWidth)

}
like image 619
dwinnbrown Avatar asked Dec 08 '22 03:12

dwinnbrown


1 Answers

Before we get to your question, several points:

Don't start the name of an instance variable with upper case.

Don't use "image" for the variable name of an image view. That suggests that it is a UIImage. It's a UIImageView, not a UIImage. Call it something like theImageView.

Now to your question: UIView objects do not have a size property (UIImage objects do have a size property. That might be the cause of your confusion.) They have a bounds rectangle and a frame rectangle. You can use theImageView.bounds.size or theImageView.frame.size.

like image 151
Duncan C Avatar answered Dec 20 '22 08:12

Duncan C