2012-06-29 15 views
0
NSString *devicetype;  

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

我有兩個圖像追加字符串通過變量傳遞客觀C到文件名

BACKGROUND_IPAD.PNG

BACKGROUND_IPHONE.PNG

現在我想要使用此字符串,如

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

這可能嗎? 如果沒有,那麼有沒有更好的方法?

回答

4

是你的使用它,但是那insread您可以將圖像的名字,當你加載圖像

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

它會自動選擇更改爲以下

Image~ipad.png 
Image~iphone.png 

現在基於您當前設備的圖像

0

是的,它的工作原理。我已經測試併成功運行。

+0

即使是這樣,使用imagenamed更好 –

+0

@Omar Abdelhafith [link] http://stackoverflow.com/questions/6623295/imagenamed-is-evil-how-to-use-the-function - 評論黑暗塵埃說,以避免使用** imagenamed **。還有其他方式會使用更多的處理,所以我認爲使用手動命名更好。 – maniclorn

+0

是在較舊的ios版本,爲新版本閱讀http://stackoverflow.com/questions/924740/dispelling-the-uiimage-imagenamed-fud –