【问题标题】:Getting iOS display resolution获取 iOS 显示分辨率
【发布时间】:2015-06-28 17:39:32
【问题描述】:

为 480x960 像素分辨率的设备设计的应用程序在 480x1136 设备上执行。在 480x1136 设备上运行时,我需要能够检测到真实分辨率。不幸的是,我没有获得设备分辨率('480x1136'),而是获得了应用程序分辨率(== '480x960'),因此[[UIScreen mainScreen] bounds].size * [[UIScreen mainScreen] scale] 返回'480x960' 而不是预期的'480x1136'

如何解决真实设备的物理分辨率?

【问题讨论】:

  • 你在什么设备上测试这个?物理设备还是模拟器?
  • 这很正常,因为就应用而言,它运行在分辨率为 480x960 的设备上。为什么需要获取设备的实际分辨率?
  • 物理设备:设计为在 3.5 英寸设备上运行的应用程序在 4 英寸设备 (iPod Touch 5,1) 上执行
  • 这样我就可以在应用程序的正确位置模拟远程生成的击键

标签: ios objective-c resolution uiscreen


【解决方案1】:
//Get device screen resolution for this specific case     

CGRect boundsOfScreen=[[UIScreen mainScreen] bounds];

        if (boundsOfScreen.size.width == 480 && boundsOfScreen.size.height == 960 ) {
            //iPod Touch 3.5-inch
        }
        else if (boundsOfScreen.size.width == 480 && boundsOfScreen.size.height == 1136) {
             //iPod Touch 4-inch
        }

【讨论】:

  • 使用上述内容,您将如何区分例如。 iPod Touch 3.5 英寸(480x960)和 iPod Touch 4 英寸(480x1136)?
  • UR 使用它的宽度解析设备类型,对于 3.5 和 4 英寸设备,宽度相同而高度不同,IMO 解决设备类型的更好方法是使用本文中描述的内容链接:stackoverflow.com/questions/11197509/…,但是,我想直接解决分辨率而不维护硬编码表映射类型到分辨率
  • 不管怎样,上面返回的是应用程序分辨率而不是物理设备分辨率
  • 我已经在三台设备和模拟器上对其进行了测试,它返回了正确的值。抱歉,如果它不适合您。为您的案例编辑了原始答案,但我相信它会起作用。
【解决方案2】:

这很丑,但是,这是我找到的唯一解决实际物理主显示分辨率的解决方案(独立于正在执行的应用程序):

#import <sys/utsname.h>

bool getDeviceNativeResolution(CGSize& res) {
    utsname si;
    uname(&si);

    if((0 == strncmp(si.machine, "iPod7", 5)) || (0 == strncmp(si.machine, "iPhone7", 7)))
        res = CGSizeMake(1080, 1920);
    else if((0 == strncmp(si.machine, "iPod6", 5)) || (0 == strncmp(si.machine, "iPhone6", 7)))
        res = CGSizeMake(750, 1334);
    else if((0 == strncmp(si.machine, "iPod5", 5)) || (0 == strncmp(si.machine, "iPhone5", 7)))
        res = CGSizeMake(640, 1136);
    else if((0 == strncmp(si.machine, "iPod4", 5)) || (0 == strncmp(si.machine, "iPhone4", 7)))
        res = CGSizeMake(640, 960);
    else
        return false;

    return true;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    相关资源
    最近更新 更多