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

标签 ios objective-c cocoa-touch

在 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/

相关文章:

ios - 在 Swift 中以编程方式在选项卡之间切换

ios - UICollectionView:重新加载数据调用 numberOfItemsInSection 但不是 cellForItemAtIndexPath

iphone - inapp 购买 - 发生未知错误 - 5002 错误代码 - iOS5

objective-c - 关于 "Semantic Issue"的编译器警告——如何解决?

ios - 使用 CorePlot 框架时显示 int 值

objective-c - 覆盖不使用 UITableView 滚动 - iOS

ios - 如何缩放和更改父 View 的原点并在 subview 中实现?

objective-c - 获取相关问题

cocoa-touch - NSImage 的 ressizedImageWithCapInsets ?

ios - 通过点击手势进行连续播放时, Storyboard会丢失导航栏