IOS: Get displayed image size in pixels

cocoaiosswift

In my app, I'm displaying an image of a rectangle from the assets library. The image is 100×100 pixels. I'm only using the 1x slot for this asset.

I want to display this image at 300×300 pixels. Doing this using points is quite simple but I can't figure out how to get UIImageView to set the size in pixels.

Alternatively, if I can't set the size in pixels to display, I'd like to get the size in pixels that the image is being displayed.

I have tried using .scale on the UIImageView and UIImage instances, but it's always 1. Even though I have set constraints to 150 and 300.

Best Answer

To get size in pixels of UIImageView:

let widthInPixels = imageView.frame.width * UIScreen.mainScreen().scale
let heightInPixels = imageView.frame.height * UIScreen.mainScreen().scale

To get size in pixels of UIImage:

let widthInPixels = image.size.width * image.scale
let heightInPixels = image.size.height * image.scale