views:

1039

answers:

3

Hi All,

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

+12  A: 

You could check the scale of the screen.

if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)] && [[UIScreen mainScreen] scale] == 2){
    //iPhone 4
}
Tom Irving
Thankx it worked
Ankur
Just be aware that scale is also available on the iPad in OS 3.2, not just the on iOS 4.
Matt Rix
Right, but the scale of an iPad isn't 2.
Tom Irving
Actually, the scale of the iPad is 2 when it's in 2x zoom mode.
Matt Rix
Very true, my mistake.
Tom Irving
A: 

I'd Love to use

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

But it won't compile on 3.1.3

I've tried something like

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {
        // Knowledge about scale (retina display)
        // Now see if we can get the scale value without a compile error on 3.1.3.

        NSMethodSignature *scaleMethodSig = [[UIScreen mainScreen] methodSignatureForSelector:@selector(scale)];
        NSInvocation *scaleInvocation = [NSInvocation invocationWithMethodSignature:(NSMethodSignature *)scaleMethodSig];
        NSUInteger length = [scaleMethodSig methodReturnLength];

        float usedScale;
        [scaleInvocation getReturnValue:&usedScale];

But failed..... Anybody know of a way to read the scale float which compiles on 3.1.3?

P5ycH0
The iPhone 4 won't be running anything less than 4.0, so there's no need to do this in 3.1.3.
Tom Irving
Tom, he may want to check to see which device he's on to decide whether to do iPhone 4 things or not. P5ycH0, are you compiling with the 3.1.3 SDK? Or with a 3.1.3 deployment target? You should be compiling your app with the 4.0 SDK, but with a 3.* deployment target.
Matt Rix
No, I am writing an app which must be able to run on 3.1.3 but also needs HiRes images if the device is capable.
P5ycH0
Yeah, compile with 4.* as your Base SDK, but with a 3.* Deployment Target, and you'll be fine.
Matt Rix
A: 

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.

Matt Rix
unfortunately, contentScaleFactor is new in iOS 4 too, so it doesn't help with backwards compatibility any more than the scale property. I wonder if it changes from 1 to 2 on-the-fly if the user clicks the "2x" button on the iPad?
Ben Gotow
It does in 3.2, so I figure it would in 4.2 as well, I haven't checked though.
Matt Rix