gpt4 book ai didi

ios - 在 iPhone App 中如何检测设备的屏幕分辨率

转载 作者:IT老高 更新时间:2023-10-28 12:19:17 27 4
gpt4 key购买 nike

在 iPhone 应用程序中,在设备上运行应用程序时如何检测正在运行应用程序的设备的屏幕分辨率?

最佳答案

CGRect screenBounds = [[UIScreen mainScreen] bounds];

这将为您提供整个屏幕的分辨率(以点为单位),因此对于 iPhone,它通常为 320x480。即使 iPhone4 的屏幕尺寸更大,iOS 仍然返回 320x480 而不是 640x960。这主要是因为较旧的应用程序出现故障。

CGFloat screenScale = [[UIScreen mainScreen] scale];

这将为您提供屏幕的比例。对于所有没有 Retina 显示屏的设备,这将返回 1.0f,而 Retina Display 设备将返回 2.0f,而 iPhone 6 Plus (Retina HD) 将返回 3.0f。

现在,如果您想获取 iOS 设备屏幕的像素宽度和高度,您只需要做一件简单的事情。

CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);

乘以屏幕的比例可以得到实际的像素分辨率。

关于iOS中点和像素之间差异的好读可以读here .

编辑:(Swift 版本)

let screenBounds = UIScreen.main.bounds
let screenScale = UIScreen.main.scale
let screenSize = CGSize(width: screenBounds.size.width * screenScale, height: screenBounds.size.height * screenScale)

关于ios - 在 iPhone App 中如何检测设备的屏幕分辨率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4779221/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com