Swift - Checking size of UIImageView

14.2k Views Asked by At

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)

}
2

There are 2 best solutions below

1
On BEST ANSWER

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.

0
On

try this

println(self.frame.height)
println(self.frame.width)