[iOS 8] LaunchImage を取得する方法

2015.04.15

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

How to get LaunchImage

アプリで起動時に表示する画像をもう少し長く表示したいと思ったことはありませんか?

デフォルトでは起動時の画像は一瞬しか表示されないため、ロゴなどをもう少し長く見せたいときは自前でその画面を作る必要があります。(たぶん) 今回は、そんなときに LaunchImage を取得する方法をお伝えします。

サンプルコード

func launchImage() -> UIImage? {
let launchImages = [
"320x480": "LaunchImage-700", // 3.5-inch Portrait
"320x568": "LaunchImage-700-568h", // 4-inch Portrait
"375x667": "LaunchImage-800-667h", // 4.7-inch Portrait
"414x736": "LaunchImage-800-Portrait-736h" // 5.5-inch Portrait
]

let width = Int(UIScreen.mainScreen().bounds.width)
let height = Int(UIScreen.mainScreen().bounds.height)
let key = "(width)x(height)"

if let imageName = launchImages[key] {
let launchImage = UIImage(named: imageName)
return launchImage
}

return nil
}

こんな感じです。

LaunchImage はアプリに組み込まれるときに以下のようなファイル名に変換されるようです。

  • LaunchImage-700
  • LaunchImage-700-568h
  • LaunchImage-800-667h
  • LaunchImage-800-Portrait-736h

上から順に 3.5インチ、4インチ、4.7インチ、5.5インチとなります。 すべて Portrait の画像です。

上記のプログラムでは実行時のデバイスに対応する LaunchImage を取得しています。 サンプルでは iPhone の Portrait のみについて書きましたが、iPad や Landscape の LaunchImage を取得することも可能です。 詳しくはこちらを御覧ください。

関連記事