0
NSString *devicetype;       

if((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)){  
devicetype = @"ipad";  
}else{    
devicetype = @"iphone";  
}

I have two images

BACKGROUND_IPAD.PNG

AND

BACKGROUND_IPHONE.PNG

Now I want to use this string like

BackGround = [CCSprite spriteWithFile:[NSString StringWithFormat:@"background_%@.png",DEVICETYPE]];

Is this possible? If not, then is there a better way?

Nick Bull
  • 4,276
  • 1
  • 18
  • 25
maniclorn
  • 1,081
  • 3
  • 12
  • 23

2 Answers2

4

Yes you an use it, but insread of that you can change the name of the image to the following

Image~ipad.png 
Image~iphone.png 

Now when you load the image

//on the iPhone and iPad use the same image name
//image named will select the image automatically
UIImage *image = [UIImage imageNamed:"Image"];

It will automatically select the image basing on your current device

Omar Abdelhafith
  • 21,163
  • 5
  • 52
  • 56
0

Yes it works. I have tested and running successfully.

maniclorn
  • 1,081
  • 3
  • 12
  • 23
  • even if it does, using imagenamed is better – Omar Abdelhafith Jun 29 '12 at 11:50
  • @Omar Abdelhafith [link]http://stackoverflow.com/questions/6623295/imagenamed-is-evil-how-to-use-the-function -- The comment by Dark dust says to avoid using **imagenamed**. and also other ways would use more processing so I think better to use manual naming. – maniclorn Jun 29 '12 at 12:15
  • that was in older ios versions, for newer versions read http://stackoverflow.com/questions/924740/dispelling-the-uiimage-imagenamed-fud – Omar Abdelhafith Jun 29 '12 at 13:25