views:

631

answers:

2

Hi, I'm just curious as to what methods people are using to dynamically use larger or smaller images in their universal iPhone/iPad apps.

I created a large test image and I tried scaling down (using cocos2d) by 0.46875. After viewing that in the iPhone 4.0 simulator I found the results were pretty crappy... rough pixel edges, etc. Plus, loading huge image files for iPhone users when they don't need them is pretty lame.

So I guess what I will probably have to do is save out two versions of every sprite... large (for the iPad side) and small (for iPhone/iPod Touch) then detect the user's device and spit out the proper sprite like so:


NSString *deviceType = [UIDevice currentDevice].model;
CCSprite *test;
if([deviceType isEqualToString:@"iPad"]) {
  test = [CCSprite spriteWithFile:@"testBigHuge.png"];
} else {
  test = [CCSprite spriteWithFile:@"testRegularMcTiny.png"];
}
[self addChild: test];

How are you guys doing this? I'd rather avoid sprinkling all of my code with if statements like this. I want to also avoid using .xib files since it's an OpenGL-based app.

Thanks!

+2  A: 

I wrote a little macro helper thingy:


#define deviceFile(file, ext) \
  ([[UIDevice currentDevice].model rangeOfString:@"iPad"].location != NSNotFound ? \
    [NSString stringWithFormat:@"%@-iPad.%@", file, ext] : \
    [NSString stringWithFormat:@"%@.%@", file, ext])

Usage:


// outputs either "test.png" or "test-iPad.png" depending on the user's device
CCSprite *test = [CCSprite spriteWithFile:deviceFile(@"test", @"png")];
taber
the only thing i need to figure out now is how to associate coords and other metadata with each image so i can easily reference/display them properly depending on the device.
taber
A: 

Consistently name every single image with either "Large" or "Small" but keep the rest of the names the same for equivalent images.

Somewhere, have a global with either the value @"Large" or @"Small" that you set on launch.

Somewhere, have a method that will take a generic name string and insert the global size specifier.

@interface CCSprite (MyProject)
+(id) spriteWithSizedFile:(NSString *)inName;
@end

@implementation CCSprite(MyProject)
+(id) spriteWithSizedFile:(NSString *)inName {
  return [CCSprite spriteWithFile:[gSizeSpecifier stringByAppendingString:inName]];
}
@end

There is probably a better place than CCSprite. This is purely an example.

drawnonward