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


130

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

Answers:


349
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);

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

可以在这里阅读有关点和像素之间的差异的很好的阅读。

编辑:(版本为Swift)

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

4
在iPhone 6 Plus的情况下,这不是一个“实际像素”分辨率。它的分辨率一切在代码渲染(除OpenGL的),具有3倍的规模,但随后被降采样,在内部以1080 X 1920的一个多好解释的画面的原始分辨率链接
RobP

可悲的是,这不会为您提供屏幕上元素的“真实”尺寸,因为Apple的“点”和“比例”只是一个近似值。(请参阅iPhone,iPad和iPad mini上的规格。)大概是为了减少现有不同组合的数量。我认为iPhone 6 Plus特别遥远。
ToolmakerSteve

实际距离不超过6+:高度736点/ 160(pt / in)= 4.60“逻辑高度;实际屏幕高度为4.79”;5%的错误。iPad更远:高度1024 pts / 160(pt / in)= 6.40“逻辑高度;实际屏幕高度为7.76”;错误20%。iPad mini还可以;它符合原始的iPhone密度。对于大多数目的来说,这意味着应该在iPad mini上测试iPad软件(以确保其可用),然后忽略大多数iPad将图像放大20%(与iPhone或iPad mini相比)的事实。
ToolmakerSteve

1
@RobP,那么您如何为iPhone 6 Plus解决此问题?
Crashalot '16

1
@Crashalot不确定“解决此问题”是什么意思吗?这取决于获得屏幕分辨率时要牢记的目的。就程序员而言,Jman012的答案是正确的,您可以渲染到1242x2208或2208x1242空间。哎呀,这甚至是我们提供启动图像的分辨率。硬件随后对该图像进行下采样并以较小的像素数将其显示在物理屏幕上的事实将是我们的代码甚至不应该意识到的“实现细节”。
RobP '16

21

UIScreen类可让您以点和像素为单位查找屏幕分辨率。

屏幕分辨率以点或像素为单位。永远不要与屏幕尺寸混淆。较小的屏幕尺寸可以具有较高的分辨率。

UIScreen的'bounds.width'返回以磅为单位的矩形大小 在此处输入图片说明

UIScreen的'nativeBounds.width'返回以像素为单位的矩形大小。该值被检测为PPI(每英寸点数)。显示设备上图像的清晰度和清晰度。 在此处输入图片说明

您可以使用UIScreen类来检测所有这些值。

迅捷3

// Normal Screen Bounds - Detect Screen size in Points.
let width = UIScreen.main.bounds.width
let height = UIScreen.main.bounds.height
print("\n width:\(width) \n height:\(height)")

// Native Bounds - Detect Screen size in Pixels.
let nWidth = UIScreen.main.nativeBounds.width
let nHeight = UIScreen.main.nativeBounds.height
print("\n Native Width:\(nWidth) \n Native Height:\(nHeight)")

安慰

width:736.0 
height:414.0

Native Width:1080.0 
Native Height:1920.0

斯威夫特2.x

//Normal Bounds - Detect Screen size in Points.
    let width  = UIScreen.mainScreen.bounds.width
    let height = UIScreen.mainScreen.bounds.height

// Native Bounds - Detect Screen size in Pixels.
    let nWidth  = UIScreen.mainScreen.nativeBounds.width
    let nHeight = UIScreen.mainScreen.nativeBounds.height

物镜

// Normal Bounds - Detect Screen size in Points.
CGFloat *width  = [UIScreen mainScreen].bounds.size.width;
CGFloat *height = [UIScreen mainScreen].bounds.size.height;

// Native Bounds - Detect Screen size in Pixels.
CGFloat *width  = [UIScreen mainScreen].nativeBounds.size.width
CGFloat *height = [UIScreen mainScreen].nativeBounds.size.width

8

在App Delegate中使用它:我正在使用情节提要

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{

if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {

    CGSize iOSDeviceScreenSize = [[UIScreen mainScreen] bounds].size;

    //----------------HERE WE SETUP FOR IPHONE 4/4s/iPod----------------------

    if(iOSDeviceScreenSize.height == 480){          

        UIStoryboard *iPhone35Storyboard = [UIStoryboard storyboardWithName:@"iPhone" bundle:nil];

        // Instantiate the initial view controller object from the storyboard
        UIViewController *initialViewController = [iPhone35Storyboard instantiateInitialViewController];

        // Instantiate a UIWindow object and initialize it with the screen size of the iOS device
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        // Set the initial view controller to be the root view controller of the window object
        self.window.rootViewController  = initialViewController;

        // Set the window object to be the key window and show it
        [self.window makeKeyAndVisible];

        iphone=@"4";

        NSLog(@"iPhone 4: %f", iOSDeviceScreenSize.height);

    }

    //----------------HERE WE SETUP FOR IPHONE 5----------------------

    if(iOSDeviceScreenSize.height == 568){

        // Instantiate a new storyboard object using the storyboard file named Storyboard_iPhone4
        UIStoryboard *iPhone4Storyboard = [UIStoryboard storyboardWithName:@"iPhone5" bundle:nil];

        // Instantiate the initial view controller object from the storyboard
        UIViewController *initialViewController = [iPhone4Storyboard instantiateInitialViewController];

        // Instantiate a UIWindow object and initialize it with the screen size of the iOS device
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

        // Set the initial view controller to be the root view controller of the window object
        self.window.rootViewController  = initialViewController;

        // Set the window object to be the key window and show it
        [self.window makeKeyAndVisible];

         NSLog(@"iPhone 5: %f", iOSDeviceScreenSize.height);
        iphone=@"5";
    }

} else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    // NSLog(@"wqweqe");
    storyboard = [UIStoryboard storyboardWithName:@"iPad" bundle:nil];

}

 return YES;
 }

5

对于iOS 8,我们可以[UIScreen mainScreen].nativeBounds像这样使用this :

- (NSInteger)resolutionX
{
    return CGRectGetWidth([UIScreen mainScreen].nativeBounds);
}

- (NSInteger)resolutionY
{
    return CGRectGetHeight([UIScreen mainScreen].nativeBounds);
}

4

请参阅《 UIScreen参考》:http : //developer.apple.com/library/ios/#documentation/uikit/reference/UIScreen_Class/Reference/UIScreen.html

if([[UIScreen mainScreen] respondsToSelector:NSSelectorFromString(@"scale")])
{
    if ([[UIScreen mainScreen] scale] < 1.1)
        NSLog(@"Standard Resolution Device");

    if ([[UIScreen mainScreen] scale] > 1.9)
        NSLog(@"High Resolution Device");
}

如果我将其放在NSLog中,请用thanx回复(@“%d”,[[UIScreen mainScreen]比例]);它给出0 ......和NSLog(@“%@”,[[UIScreen mainScreen] scale]); 它给了零个Pls,让我知道了如何在模拟器上运行时获得屏幕分辨率或如何测试它是否提供了正确的分辨率
ios

2
试试NSLog(@"%f",[[UIScreen mainScreen] scale]);
vikingosegundo

0

使用此代码将有助于获取任何类型的设备的屏幕分辨率

 [[UIScreen mainScreen] bounds].size.height
 [[UIScreen mainScreen] bounds].size.width
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.