在iPhone SDK中检测视网膜屏幕/ iPhone 4

Rob*_*bin 23 iphone cocoa-touch iphone-4

在我的应用程序中,我正在从网上下载一些图像(从我的服务器到精确),为了节省一些带宽,尤其是手机上的内存,我提供两种分辨率:480x320用于"旧"iPhone系列和带有视网膜显示屏的iPhone 4为960x640.现在,我需要能够在应用程序中检测它何时在支持视网膜屏幕的设备上运行.我怎么能这样做?

我一直在考虑使用下面的代码片段,它会返回一个特定的设备标识符,例如."iPhone3",然后我将限制检测到iPhone4,并需要为具有视网膜显示器的任何后续设备更新该代码.

size_t size;

// Set 'oldp' parameter to NULL to get the size of the data
// returned so we can allocate appropriate amount of space
sysctlbyname("hw.machine", NULL, &size, NULL, 0); 

// Allocate the space to store name
char *name = malloc(size);

// Get the platform name
sysctlbyname("hw.machine", name, &size, NULL, 0);

// Place name into a string
NSString *machine = [NSString stringWithCString:name];
Run Code Online (Sandbox Code Playgroud)

有没有更好的选择(也许这是非常明显但我错过了它)?

Rob*_*bin 32

刚刚在官方Apple开发者论坛上做了一些阅读,并且已经在那里详细讨论了这些问题.对我来说,最好的方式似乎是使用scale属性UIScreen.虽然只适用于iOS 4以后,它会告诉你你需要知道的一切,将在未来最有可能发挥更为重要的作用(已经注意到iPad的1024×768的屏幕分辨率是完全十五分之三十二分辨率480x320*?) .

UIScreen.mainScreen.scale
Run Code Online (Sandbox Code Playgroud)

如果你有另一个想法随意张贴:)

  • 是的,使用它,不要试图查询你正在运行的设备,除了痛苦之外什么都不会结束. (2认同)

小智 23

以下是一些代码,以正确的方式为iOS 3.x和4.x执行:

BOOL hasHighResScreen = NO;
if ([UIScreen instancesRespondToSelector:@selector(scale)]) {
    CGFloat scale = [[UIScreen mainScreen] scale];
    if (scale > 1.0) {
        hasHighResScreen = YES;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • iPad的"iphone模拟器"上有2x问题的解决方法吗? (2认同)

Gun*_*nds 13

Scott Gustafson对答案的一点回应:

如果我们需要区分iPad/Retina/iphone:

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
    {
        CGFloat scale = [[UIScreen mainScreen] scale];

           if (scale > 1.0) 
           {
                //iPad retina screen
           }  
           else
           {
                //iPad screen
           }
    } 
    else
    {
         if ([UIScreen instancesRespondToSelector:@selector(scale)]) 
         {
               CGFloat scale = [[UIScreen mainScreen] scale];

               if (scale > 1.0) 
               {
                    if([[ UIScreen mainScreen ] bounds ].size.height == 568)
                    {
                        //iphone 5
                    }
                    else
                    {
                        //iphone retina screen
                    }
               }
               else
               {
                    //iphone screen
               }
         }
    }
Run Code Online (Sandbox Code Playgroud)


Pap*_*ter 5

我通过这种方式获得真正的屏幕尺寸(以像素为单位):

UIScreen *MainScreen = [UIScreen mainScreen];
UIScreenMode *ScreenMode = [MainScreen currentMode];
CGSize Size = [ScreenMode size]; // <--- Real screen size
Run Code Online (Sandbox Code Playgroud)