2
votes

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!

3
Oh, awesome, I just realized you can name your files the same name and, depending on which Resources group you add them to in your xcode project, it will automagically decide which one to display! So if you put test.png inside "Resources-iPad" the iPad will display that image. Save out a new smaller .png and throw it in the "Resources" group and that will be displayed on the iPhone. Cool!taber
Actually, now that I've compile the project 4 or 5 times in a row, it seems to randomly select between the Resources/test.png image and the Resources-iPad/test.png image. Weird. If anyone else has some suggestions I'd love to hear 'em, thanks.taber

3 Answers

4
votes

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")];
0
votes

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.

0
votes
#define M_IS_IPAD (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
and make a function like this

   +(id)BooL
{
   if(M_IS_IPAD)
  {
      return true;
  }
  else
  {
    return false;
  }

}

use this anywhere:) and change images for ipad or ipod accordingly to this....

Regards: shauket