如何区分iphone4和iphone 3

发布于 2024-09-10 20:23:38 字数 259 浏览 4 评论 0原文

我正在尝试使用 cocos2d 引擎为 iphone 构建游戏。我想知道如何区分用户使用的是 iphone 4 还是 iphone 3,因为我想为 iphone4 加载高分辨率图形,为 iphone 3 加载低分辨率图形。我知道我是否在以下位置使用 @2x.png如果我使用的是 iphone 4,则图像文件名 UIImage 的末尾会自行加载高分辨率图像,但对于游戏,我使用 cocos2d 引擎的 CCSprite 类来加载图形。

我非常感谢您的回复。

问候, 安库尔

I am trying to build a game for the iphone using cocos2d engine. I wanted to know how can I tell a difference whether the user is using iphone 4 or iphone 3 as I wanted to load hi-resolution graphics for the iphone4 and low-resolution for iphone 3. I know if I use @2x.png at the end of the image file name UIImage loads the hi-resolution image by itself if I am using an iphone 4 but for the game I am using cocos2d engine's CCSprite class to load the graphics.

I would really appreciate the reply.

Regards,
Ankur

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

花间憩 2024-09-17 20:23:38

您可以检查屏幕的比例。

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

You could check the scale of the screen.

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){
    //iPhone 4
}
奢华的一滴泪 2024-09-17 20:23:38

用于检测包括新 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;
}

来源:Adriano Paladini
http://developer.appcelerator.com/question/133826/detecting-新-ipad-3-dpi-和-retina

For detecting retina display on all devices including the new 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;
}

Credit to: Adriano Paladini
http://developer.appcelerator.com/question/133826/detecting-new-ipad-3-dpi-and-retina

回忆躺在深渊里 2024-09-17 20:23:38
- (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";  
}  
- (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";  
}  
三人与歌 2024-09-17 20:23:38

不管苹果文档如何说,UIScreen 的缩放属性不仅在 iOS4 中可用,在 iPad 3.2 中也可用。这意味着它可能是一种不可靠的检查您所使用的设备的方法。

相反,您应该检查 contentScaleFactor 在主窗口(或任何 UIView)上是否可用,然后检查比例值。

Despite what Apple's docs say, the UIScreen's scale property isn't just available in iOS4, it's also available in 3.2 on the iPad. This means it's probably an unreliable way to check which device you're on.

Instead, you should check if contentScaleFactor is available on your main window(or any UIView), and then check the scale value.

昵称有卵用 2024-09-17 20:23:38

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

我已经在 UIScreen 上创建了类别来执行此操作。有关更详细的说明,请参阅我对检测视网膜显示屏的回答。这是代码:

@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
}

Checking the scale property is not sufficient, as on iPad 3.2 in 2x mode, the scale property exists and will return 2.0, but we know that device does NOT have a Retina display.

I've created at category on UIScreen to do this. For a more detailed explanation, see my answer to Detect Retina Display. Here's the code:

@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

Usage example:

if ([UIScreen mainScreen] zb_isRetina) {
  // Retina display
}
泪之魂 2024-09-17 20:23:38

只需添加我的 2 美分:

我明白你在这里做什么,但是将其绑定到像 2.0 这样的特定值目前是好的,但是如果例如下一个 iPad 的分辨率有像 x1.5 这样的提升怎么办?对我来说,任何高于 1.0 的显示都比正常显示更高,因此我将加载高分辨率图形。如果是 iPad、iPhone 也没什么关系....

just adding my 2 cents :

I see what you are doing here, but binding this to a specific value like 2.0 is good for the moment, but what if for example next iPad got resolution bump like x1.5 ? For me anything that goes above 1.0 is kind of higher-than-normal display so I will load hi-res graphics. Does not matter much then if that is iPad,iPhone ....

对你的占有欲 2024-09-17 20:23:38

我知道这个话题现在有点老了,但它可能对一些人有帮助。
在 Cocos2d 上,您可以使用文件上的 -hd 后缀加载 iphone4 的高分辨率图形和 iphone 3 的低分辨率图形。

您只需像这样启用视网膜显示:

// 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");

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

I know that the topic is a bit old now, but It might help some people.
On Cocos2d you can load hi-resolution graphics for the iphone4 and low-resolution for iphone 3 using the -hd suffix on your files.

You just have to enable the retina display like this before:

// 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");

For more information read the documentation here: RetinaDisplay in cocos2d

情深已缘浅 2024-09-17 20:23:38

导入“UIScreen+Retina.h”

if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
    //IPAD        
    if ([[UIScreen mainScreen] isRetina]) {
        // IPAD 3 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_HIGHRES;            
    }else{
        //iPAD 1/2
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_LOWRES;        }
}else{
    //IPHONE
    if ([[UIScreen mainScreen] isRetina]) {
        // IPHONE 4/4s/5 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_HIGHRES;

    }else{
        //IPHONE (3.x)
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_LOWRES;

    }
}

import "UIScreen+Retina.h"

if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad){
    //IPAD        
    if ([[UIScreen mainScreen] isRetina]) {
        // IPAD 3 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_HIGHRES;            
    }else{
        //iPAD 1/2
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPAD_LOWRES;        }
}else{
    //IPHONE
    if ([[UIScreen mainScreen] isRetina]) {
        // IPHONE 4/4s/5 - Retina display
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_HIGHRES;

    }else{
        //IPHONE (3.x)
        bannersGetPromoServerRequest.size = kXML_API_IMAGESIZE_IPHONE_LOWRES;

    }
}
少女的英雄梦 2024-09-17 20:23:38

缩放适用于 iPad,但是您始终可以使用 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 来检查它是 iPad 还是 iPhone/iTouch

scale is available for iPad, however you can always use if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) to check whether it is iPad or iPhone/iTouch

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文