什么是检测ios应用程序是否在iPhone,iPhone Retina显示器或iPad中运行的代码?

Gre*_*reg 10 iphone scale uiimage ipad ios

什么是检测ios应用程序是否在iPhone,iPhone Retina显示器或iPad中运行的代码?

背景:

  • 对于我的iPhone应用程序,我在XCode目标/摘要页面中定义了特定图像:iPhone启动图像,iPhone视网膜显示启动图像,iPad纵向和iPad横向.

  • 在主视图中有一个UIImageView子视图我用于背景图像 - 目前我通过选择我用于iPhone启动图像的图像在XCode(非编程)中指定它.

所以我问如何判断我在哪一个运行,以便在viewDidLoad中我可以加载适当的分辨率背景图像.然后应用程序启动的背景图像与应用程序主屏幕启动后的背景应该是无缝过渡.

omz*_*omz 18

您可以使用它[[UIDevice currentDevice] userInterfaceIdiom]来确定您是在iPhone/iPod touch还是iPad上运行.

通常无需直接确定您是否在视网膜显示器上,因为UIImage在您使用时会自动处理imageNamed并将"@ 2x"附加到您的高分辨率图像文件名中(请参阅iOS图纸和打印指南中的支持高分辨率屏幕)).

如果你真的需要知道屏幕具有高分辨率,使用UIScreenscale方法.

  • 注意`[[UIDevice currentDevice] userInterfaceIdiom]`可以查看`UIUserInterfaceIdiomPhone`或`UIUserInterfaceIdiomPad` (8认同)

Luk*_*uke 8

这是我使用的两种有用的类方法,它直接回答了您的问题 - 您可能希望进一步使用该方法:

+(BOOL)isPad
{
#ifdef UI_USER_INTERFACE_IDIOM
    return UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad;
#endif
    return NO;
}

+(BOOL)hasRetinaDisplay
{
    // checks for iPhone 4. will return a false positive on iPads, so use the above function in conjunction with this to determine if it's a 3GS or below, or an iPhone 4.
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2)
        return YES;
    else
        return NO;
}
Run Code Online (Sandbox Code Playgroud)