Misha M Misha M - 4 months ago 90
Swift Question

iOS: Get displayed image size in pixels

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

I want to display this image at 300x300 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

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


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