2013-01-13 41 views
0

我在寫一個支持旋轉的通用應用程序。當應用程序啓動時,它需要從互聯網下載一些數據,所以我推送一個帶有活動指示器的UIViewController,因爲它不可能具有動畫啓動圖像或添加標籤或對象。iOS通用應用程序啓動圖像

我想「加載」VC具有與啓動相同的bg圖像,但是,因爲它是通用應用程序,所以我無法設置簡單的[UIImage imageNamed:@「Default.png」],因爲它可以在iPhone或iPad上運行,並且iPad可以縱向或橫向啓動(iPhone應用程序始終以縱向顯示)。

問題是:有一種方法可以知道哪個Default.png被用作啓動圖像?它可以是

  • 爲Default.png(@ 2×如果視網膜)
  • 默認-Portrait.png(@ 2×如果視網膜)
  • 默認-Landscape.png(@ 2×如果視網膜)
  • 默認[email protected]

如果有現在這樣,我會檢查currentDevice和方向,並設定手動imageNamed。

感謝, 最大

回答

1

有縱向和橫向沒有後綴。你將不得不手動檢查方向與[[UIDevice currentDevice] orientation]

爲了顯示iPad和iPhone/iPod Touch的不同圖像,您可以將~ipad添加到iPad圖像的末尾,並將~iphone添加到iPhone/iPod Touch圖像的末尾。例如:

默認〜iphone.png將在iPhone/iPod Touch和默認加載〜ipad.png將在iPad上加載此:

[UIImage imageNamed:@"Default.png"]; 

還爲iPhone 5沒有說明符雖然。所以你必須檢查[[UIScreen mainScreen] bounds].size.height並再次手動加載UIImage


FULL(未經測試)例:

UIImage *image; 

if ([UIScreen mainScreen].bounds.size.height == 568.0) 
{ 
    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) 
    { 
     image = [UIImage imageNamed:@"Default-568h-Landscape"]; 
    } 
    else 
    { 
     image = [UIImage imageNamed:@"Default-568h-Portrait"]; 
    } 
} 
else 
{ 
    if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) 
    { 
     image = [UIImage imageNamed:@"Default-Landscape"]; 
    } 
    else 
    { 
     image = [UIImage imageNamed:@"Default-Portrait"]; 
    } 
} 
// check suggested by Guy Kogus 
if (image == nil) image = [UIImage imageNamed:@"Default"]; 

要回答你的問題的意見:

不,你不能在運行時查詢使用的是什麼啓動圖像。

+0

有一個用於縱向或橫向的後綴。如果你想看一定的話,創建一個虛擬項目,並有2個圖像,一個1024x768,另一個768x1024。轉到項目設置並拖動啓動圖像部分中的圖像,您會看到Xcode自動使用所需的-Landscape或-Portrait後綴命名它們(或者足夠了嗎?)。 –

+0

是的,但我不認爲在使用' - [UIImage imageNamed:]' –

+0

時工作不行,您必須手動添加這些後綴。但這很容易,你只需要使用狀態欄的方向。 –

0

@ 2x後綴是自動選擇的,因此您不必擔心這一點。有幾項檢查需要執行:

- (NSString *)defaultImage 
{ 
    NSString *imageName = nil; 
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) 
    { 
     if ([UIScreen mainScreen].bounds.size.height == 568.0) 
     { 
      imageName = @"Default-568h"; 
     } 
     else 
     { 
      imageName = @"Default"; 
     } 
    } 
    else // ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) 
    { 
     if (UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation)) 
     { 
      imageName = @"Default-Landscape"; 
     } 
     else // if (UIInterfaceOrientationIsPortrait([UIApplication sharedApplication].statusBarOrientation)) 
     { 
      imageName = @"Default-Portrait"; 
     } 
    } 
    return [UIImage imageNamed:imageName]; 
} 
+0

謝謝,看我對博的評論答案。 – masgar