中如何检测设备的屏幕分辨率

中如何检测设备的屏幕分辨率

本文介绍了在 iPhone App 中如何检测设备的屏幕分辨率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In iPhone App,while running the App on device How to detect the screen resolution of the device on which App is running?

推荐答案

CGRect screenBounds = [[UIScreen mainScreen] bounds];

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

That will give you the entire screen's resolution in points, so it would most typically be 320x480 for iPhones. Even though the iPhone4 has a much larger screen size iOS still gives back 320x480 instead of 640x960. This is mostly because of older applications breaking.

CGFloat screenScale = [[UIScreen mainScreen] scale];

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

This will give you the scale of the screen. For all devices that do not have Retina Displays this will return a 1.0f, while Retina Display devices will give a 2.0f and the iPhone 6 Plus (Retina HD) will give a 3.0f.

现在如果你想获得像素宽度 &iOS 设备屏幕的高度,你只需要做一件简单的事情.

Now if you want to get the pixel width & height of the iOS device screen you just need to do one simple thing.

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

通过乘以屏幕的比例,您可以获得实际的像素分辨率.

By multiplying by the screen's scale you get the actual pixel resolution.

可以阅读有关 iOS 中点和像素之间差异的良好阅读 这里.

A good read on the difference between points and pixels in iOS can be read 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)

这篇关于在 iPhone App 中如何检测设备的屏幕分辨率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:06