【问题标题】:iOS get physical screen size programmatically?iOS以编程方式获取物理屏幕尺寸?
【发布时间】:2012-01-05 16:12:18
【问题描述】:

这可能吗?我想要英寸数,而不是像素数。我知道它大约是 160 ppi。但不完全是。

【问题讨论】:

  • 您可能已经意识到这一点,但是基于设备的物理特性编写代码可能会在未来引起问题。如果下一代 iPhone 的屏幕稍大一些,但分辨率保持不变(正如一些谣言所暗示的那样),那么您的代码将在该设备上被破坏。
  • 有办法get the screen size on OS X,不知道iOS上能用吗?
  • @adib 您发表评论已经有一段时间了,但遗憾的是 CGDisplayScreenSize 仅在 macOS 上可用

标签: iphone ios ipad screen screen-size


【解决方案1】:

没有任何 API 可以为您提供此功能。最好的办法是查看设备的屏幕尺寸(以磅为单位),并据此推测它是 iPad 还是 iPhone 等,然后使用硬编码值作为屏幕尺寸。

下面是一些获取屏幕大小的代码:

CGRect screenRect = [[UIScreen mainScreen] bounds];
CGFloat screenWidth = screenRect.size.width;
CGFloat screenHeight = screenRect.size.height;

请注意,宽度和高度可能会交换,具体取决于设备方向。

【讨论】:

    【解决方案2】:

    如果可用,它将位于UIScreenUIDevice,但它不存在。

    您可以从Erica's UIDevice-extension 中的信息和here on Wikipedia 列出的每个设备的规格中推断出来。

    【讨论】:

      【解决方案3】:

      这是一种估算设备屏幕尺寸的简短方法。它已针对最新设备进行了更新,但可能会在将来的设备上失败(所有猜测方法都可能)。如果设备被镜像也会混淆(返回设备的屏幕大小,而不是镜像的屏幕大小)

      #define SCREEN_SIZE_IPHONE_CLASSIC 3.5
      #define SCREEN_SIZE_IPHONE_TALL 4.0
      #define SCREEN_SIZE_IPAD_CLASSIC 9.7
      
      + (CGFloat)screenPhysicalSize
      {
          if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
          {
              CGSize result = [[UIScreen mainScreen] bounds].size;
              if (result.height < 500)
                  return SCREEN_SIZE_IPHONE_CLASSIC;  // iPhone 4S / 4th Gen iPod Touch or earlier
              else
                  return SCREEN_SIZE_IPHONE_TALL;  // iPhone 5
          }
          else
          {
              return SCREEN_SIZE_IPAD_CLASSIC; // iPad
          }
      } 
      

      【讨论】:

      • 当然,iPad Mini 的推出打破了这个密码(它的物理尺寸比 iPad 2 小,但分辨率相同)。这可以作为指导,但不能保证..
      • 现在您拥有 iPhone 6 和 6+,用户可以在“缩放”模式下运行它们,因此同一部手机可以报告不同的边界值。
      【解决方案4】:

      您可能需要使用[UIScreen mainScreen].scale;

      CGFloat scale = [UIScreen mainScreen].scale;
      CGRect screenRect = [[UIScreen mainScreen] bounds];
      
      CGFloat physicalWidth = screenRect.size.width * scale;
      CGFloat physicalHeight = screenRect.size.height * scale;
      

      【讨论】:

        【解决方案5】:

        也许 Jeff Hay 的代码可以适应 iPad Mini。诀窍是获取设备的型号标识符。最新的非视网膜 iPad 是“iPad2,4”,第一款 iPad mini 是“iPad2,5”。现在您只需要检查屏幕缩放比例是否为 1.0(非视网膜)

        虽然此代码不是面向未来的,但您始终可以为模型标识符添加更多规则。

        #import <sys/utsname.h>
        
        #define SCREEN_SIZE_IPAD_MINI 7.9
        
            struct utsname systemInfo;
            uname(&systemInfo);
            if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad && strcasecmp(systemInfo.machine, "iPad2,5") >= 0 [[UIScreen mainScreen] scale] == 1.0)
                return SCREEN_SIZE_IPAD_MINI
        

        【讨论】:

          【解决方案6】:

          我使用的“公式”是

          #define IS_iPhone5 ( fabs( (double)[ [ UIScreen mainScreen ] bounds ].size.height - (double)568 ) < DBL_EPSILON )
          

          【讨论】:

            【解决方案7】:

            自从提出这个问题以来,我创建了一个开源库来处理这个问题:IRLSize。它可以用于任一方向:以真实世界的维度测量视图(或整个屏幕)的大小,或将视图的大小设置到特定的真实世界维度。

            【讨论】:

            • 他可能考虑过这部分研究。至少帮助他指出正确的方向。
            • @eddieroger 这是两年半前的评论。来吧,伙计。
            【解决方案8】:

            注意:屏幕旋转在这里很重要

            extension UIScreen {
                var physicalSize:CGSize {
                    return CGSize(width: bounds.width*scale, height: bounds.height*scale)
                }
            }
            

            使用:

            print(UIScreen.main.physicalSize)
            

            【讨论】:

              【解决方案9】:

              这是获取屏幕尺寸的 Swift 方法:

              var screenWidth: CGFloat {
                  if UIInterfaceOrientationIsPortrait(screenOrientation) {
                      return UIScreen.mainScreen().bounds.size.width
                  } else {
                      return UIScreen.mainScreen().bounds.size.height
                  }
              }
              var screenHeight: CGFloat {
                  if UIInterfaceOrientationIsPortrait(screenOrientation) {
                      return UIScreen.mainScreen().bounds.size.height
                  } else {
                      return UIScreen.mainScreen().bounds.size.width
                  }
              }
              var screenOrientation: UIInterfaceOrientation {
                  return UIApplication.sharedApplication().statusBarOrientation
              }
              

              这些作为标准功能包含在:

              https://github.com/goktugyil/EZSwiftExtensions

              【讨论】:

              • 这并没有回答他的问题,因为他明确表示“我想要的是英寸数,而不是像素数。”
              • 在这种情况下没有一个答案。但至少我们知道 iphone 有多少英寸,所以很容易从这里计算出来
              【解决方案10】:

              没有人提到fixedCoordinateSpace。在 Swift 3 中,要获得纵向向上的屏幕尺寸,您应该使用:UIScreen.main.fixedCoordinateSpace.bounds

              【讨论】:

                【解决方案11】:

                CGFloat scale = [UIScreen mainScreen].scale; CGRect screenRect = [[UIScreen mainScreen] bounds]; CGFloat screenWidth = screenRect.size.width * (scale/100.0f); CGFloat screenHeight = screenRect.size.height * (scale/100.0f);

                • 规模存在!

                【讨论】:

                  猜你喜欢
                  • 2013-01-06
                  • 1970-01-01
                  • 1970-01-01
                  • 2020-04-29
                  • 1970-01-01
                  • 1970-01-01
                  • 2013-07-03
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多