iOS:以像素为单位显示图像大小

Mis*_*a M 11 cocoa ios swift

在我的应用程序中,我正在显示资产库中的矩形图像.图像为100x100像素.我只使用这个资产的1x插槽.

我想以300x300像素显示此图像.使用点执行此操作非常简单,但我无法弄清楚如何让UIImageView设置像素大小.

或者,如果我无法设置要显示的像素大小,我想获得显示图像的像素大小.

我已尝试.scale在UIImageView和UIImage实例上使用,但它总是1.尽管我已将约束设置为150和300.

Cod*_*ode 29

获得以像素为单位的大小UIImageView:

let widthInPixels = imageView.frame.width * UIScreen.mainScreen().scale
let heightInPixels = imageView.frame.height * UIScreen.mainScreen().scale
Run Code Online (Sandbox Code Playgroud)

获得以像素为单位的大小UIImage:

let widthInPixels = image.size.width * image.scale
let heightInPixels = image.size.height * image.scale
Run Code Online (Sandbox Code Playgroud)

  • 在 Swift 5 中,使用“UIScreen.main.scale”。 (3认同)