【问题标题】:in iPhone App How to detect the screen resolution of the device在 iPhone App 中如何检测设备的屏幕分辨率
【发布时间】:2026-01-12 22:45:01
【问题描述】:

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

【问题讨论】:

    标签: ios objective-c cocoa-touch


    【解决方案1】:
    CGRect screenBounds = [[UIScreen mainScreen] bounds];
    

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

    CGFloat screenScale = [[UIScreen mainScreen] scale];
    

    这将为您提供屏幕的比例。对于所有没有 Retina Displays 的设备,这将返回 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)
    

    【讨论】:

    • 除了这不是 iPhone 6 Plus 的“实际像素”分辨率。它是所有在代码中呈现的分辨率(OpenGL 除外),具有 3 倍比例,但随后 在内部被下采样到屏幕的原始分辨率 1080 x 1920 . link 的许多很好的解释之一
    • 遗憾的是,这不会为您提供屏幕上元素的“真实”尺寸,因为 Apple 的“点”和“比例”概念只是一个近似值。 (请参阅 iPhone、iPad 和 iPad mini 的规格。)大概是为了减少存在的不同组合的数量。我认为 iPhone 6 Plus 离我们特别遥远。
    • 其实 6+ 也不算太远:高度 736 pts / 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 相比)迷你)。
    • @RobP 那么你如何为 iPhone 6 Plus 解决这个问题?
    • @Crashalot 不确定“解决这个问题”是什么意思?这取决于您获得屏幕分辨率时的目的。就程序员而言,Jman012 的回答是正确的,你渲染成 1242x2208 或 2208x1242 的空间。哎呀,这甚至是我们提供启动图像的分辨率。硬件随后对该图像进行采样并将其显示在具有较小像素数的物理屏幕上这一事实将是我们的代码甚至不应该意识到的“实现细节”。
    【解决方案2】:

    UIScreen 类可让您在点和像素中找到屏幕分辨率。

    屏幕分辨率以点或像素为单位。它不应该与屏幕尺寸相混淆。较小的屏幕尺寸可以具有较高的分辨率。

    UIScreen 的“bounds.width”以点为单位返回矩形大小

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

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

    Swift3

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

    Swift 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
    

    ObjectiveC

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

    【讨论】:

      【解决方案3】:

      在 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;
       }
      

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          【解决方案5】:

          请参阅 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(@"%d",[[UIScreen mainScreen] scale]);它给出 0 ...... 和 NSLog(@"%@",[[UIScreen mainScreen] scale]);它给了 nil 请让我知道如何获得屏幕分辨率或如何在模拟器上运行它时测试它是否给出正确的分辨率
          • 试试NSLog(@"%f",[[UIScreen mainScreen] scale]);
          【解决方案6】:

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

           [[UIScreen mainScreen] bounds].size.height
           [[UIScreen mainScreen] bounds].size.width
          

          【讨论】: