Ios 如何区分iphone4和iPhone3

Ios 如何区分iphone4和iPhone3,ios,ccsprite,Ios,Ccsprite,我正在尝试使用cocos2d引擎为iphone构建一个游戏。我想知道,当我想为iphone4加载高分辨率图形和为iPhone3加载低分辨率图形时,如何区分用户使用的是iphone4还是iPhone3。我知道如果我在图像文件名末尾使用@2x.png,如果我使用的是iphone 4,UIImage会自动加载高分辨率图像,但对于游戏,我使用cocos2d引擎的CCSprite类加载图形 我非常感谢你的答复 问候,, Ankur您可以检查屏幕的比例 if ([[UIScreen mainScreen]

我正在尝试使用cocos2d引擎为iphone构建一个游戏。我想知道,当我想为iphone4加载高分辨率图形和为iPhone3加载低分辨率图形时,如何区分用户使用的是iphone4还是iPhone3。我知道如果我在图像文件名末尾使用@2x.png,如果我使用的是iphone 4,UIImage会自动加载高分辨率图像,但对于游戏,我使用cocos2d引擎的CCSprite类加载图形

我非常感谢你的答复

问候,,
Ankur

您可以检查屏幕的比例

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){
    //iPhone 4
}

不管苹果的文档怎么说,UIScreen的scale属性不仅在iOS4中可用,在iPad上也可以在3.2中使用。这意味着,这可能是一种不可靠的方式来检查你的设备上


相反,您应该检查主窗口(或任何UIView)上是否有contentScaleFactor,然后检查缩放值

检查
scale
属性是不够的,因为在2x模式下的iPad 3.2上,
scale
属性存在并将返回2.0,但我们知道该设备没有视网膜显示器

我在
ui屏幕上创建了category来实现这一点。有关更详细的解释,请参见我对的回答。代码如下:

@interface UIScreen(ZBScreenRetinaAdditions)

// Returns YES if this is a Retina display.
- (BOOL)zb_isRetina;

@end

@implementation UIScreen(ZBScreenRetinaAdditions)

- (BOOL)zb_isRetina {
  return [self respondsToSelector:@selector(displayLinkWithTarget:selector:)] && (self.scale == 2.0);
}

@end
用法示例:

if ([UIScreen mainScreen] zb_isRetina) {
  // Retina display
}

加上我的2美分:


我知道你在这里做什么,但是把它绑定到一个特定的值,比如2.0,目前是很好的,但是如果下一个iPad得到了像x1.5这样的分辨率凹凸,会怎么样呢?对我来说,任何超过1.0的东西都比正常显示高,所以我会加载高分辨率的图形。如果是iPad、iPhone……那没什么大不了的。

我知道这个话题现在有点老了,但它可能会帮助一些人。
- (NSString *) platform  
{  
    size_t size;  
    sysctlbyname("hw.machine", NULL, &size, NULL, 0);  
    char *machine = malloc(size);  
    sysctlbyname("hw.machine", machine, &size, NULL, 0);  
    NSString *platform = [NSString stringWithCString:machine];  
    free(machine);  
    return platform;  
}  

- (NSString *) platformString  
{  
    NSString *platform = [self platform];  
    if ([platform isEqualToString:@"iPhone1,1"]) return @"Original iPhone";  
    if ([platform isEqualToString:@"iPhone1,2"]) return @"iPhone 3G";  
    if ([platform isEqualToString:@"iPhone2,1"]) return @"iPhone 3G[S]"; 
    if ([platform isEqualToString:@"iPhone3,1"]) return @"iPhone 4";   
    return @"Unknown";  
}  
在Cocos2d上,您可以使用文件中的-hd后缀为iphone4加载高分辨率图形,为iPhone3加载低分辨率图形

您只需像以前一样启用视网膜显示:

// Enables High Res mode (Retina Display) on iPhone 4 and maintains low res on all other devices
if( ! [director enableRetinaDisplay:YES] )
    CCLOG(@"Retina Display Not supported");

有关更多信息,请阅读此处的文档:

缩放适用于iPad,但您始终可以使用if(UI\u USER\u INTERFACE\u IDIOM()==UIUserInterfaceIdiomPad)检查它是iPad还是iPhone/iTouch

导入“UIScreen+Retina.h”
用于检测所有设备上的视网膜显示,包括新款iPad

    +(BOOL)isRetinaDisplay {
    // since we call this alot, cache it
    static CGFloat scale = 0.0;
    if (scale == 0.0) {
        // NOTE: In order to detect the Retina display reliably on all iOS devices,
        // you need to check if the device is running iOS4+ and if the 
        // [UIScreen mainScreen].scale property is equal to 2.0. 
        // You CANNOT assume a device is running iOS4+ if the scale property exists,
        // as the iPad 3.2 also contains this property.
        // On an iPad running iOS3.2, scale will return 1.0 in 1x mode, and 2.0
        // in 2x mode -- even though we know that device does not contain a Retina display.
        // Apple changed this behavior in iOS4.2 for the iPad: it returns 1.0 in both
        // 1x and 2x modes. You can test this yourself in the simulator.
        // I test for the -displayLinkWithTarget:selector: method on the main screen
        // which exists in iOS4.x but not iOS3.2, and then check the screen's scale:

        if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && 
            ([UIScreen mainScreen].scale == 2.0)) {
            scale = 2.0;
            return YES;
        } else {
            scale = 1.0;
            return NO;
        }   

    }
    return scale > 1.0;
}
荣誉:阿德里亚诺·帕拉迪尼

请注意,在操作系统3.2中,iPad上也有缩放功能,而不仅仅是iOS 4。对,但iPad的缩放功能不是2。事实上,当它处于2倍缩放模式时,iPad的缩放功能是2。但是,如果你在2倍缩放模式下使用iPad,那么提供更高分辨率的图形不是一个不错的奖励吗?正如上面的评论所述,这在iPad3.2上返回一个假阳性(但在iOS4.x设备上正常工作)。不幸的是,contentScaleFactor在iOS 4中也是新的,因此它对向后兼容性的帮助不亚于scale属性。我想知道如果用户点击iPad上的“2x”按钮,它是否会从1变为2?在3.2中是这样的,所以我想在4.2中也是这样,我还没有检查。只是好奇。。。为什么有
?是:结尾处没有
?在你的代码中没有必要,对吗?你是对的,没有必要。这种风格是我第一次学习Objective-C时从一些例子中养成的一个坏习惯。
    +(BOOL)isRetinaDisplay {
    // since we call this alot, cache it
    static CGFloat scale = 0.0;
    if (scale == 0.0) {
        // NOTE: In order to detect the Retina display reliably on all iOS devices,
        // you need to check if the device is running iOS4+ and if the 
        // [UIScreen mainScreen].scale property is equal to 2.0. 
        // You CANNOT assume a device is running iOS4+ if the scale property exists,
        // as the iPad 3.2 also contains this property.
        // On an iPad running iOS3.2, scale will return 1.0 in 1x mode, and 2.0
        // in 2x mode -- even though we know that device does not contain a Retina display.
        // Apple changed this behavior in iOS4.2 for the iPad: it returns 1.0 in both
        // 1x and 2x modes. You can test this yourself in the simulator.
        // I test for the -displayLinkWithTarget:selector: method on the main screen
        // which exists in iOS4.x but not iOS3.2, and then check the screen's scale:

        if ([[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && 
            ([UIScreen mainScreen].scale == 2.0)) {
            scale = 2.0;
            return YES;
        } else {
            scale = 1.0;
            return NO;
        }   

    }
    return scale > 1.0;
}