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 coc开发者_如何学Pythonos2d) 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!
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")];
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.
#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
精彩评论