iOS 7 / Xcode 5: Access device launch images programmatically
You can copy/paste the following code to load your app's launch image at runtime:
// Load launch image
NSString *launchImageName;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone)
{
if ([UIScreen mainScreen].bounds.size.height == 480) launchImageName = @"[email protected]"; // iPhone 4/4s, 3.5 inch screen
if ([UIScreen mainScreen].bounds.size.height == 568) launchImageName = @"[email protected]"; // iPhone 5/5s, 4.0 inch screen
if ([UIScreen mainScreen].bounds.size.height == 667) launchImageName = @"[email protected]"; // iPhone 6, 4.7 inch screen
if ([UIScreen mainScreen].bounds.size.height == 736) launchImageName = @"[email protected]"; // iPhone 6+, 5.5 inch screen
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
{
if ([UIScreen mainScreen].scale == 1) launchImageName = @"LaunchImage-700-Portrait~ipad.png"; // iPad 2
if ([UIScreen mainScreen].scale == 2) launchImageName = @"LaunchImage-700-Portrait@2x~ipad.png"; // Retina iPads
}
self.launchImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:launchImageName]];
You can use the launch images without having to include them twice. The key is that when you use an asset catalog, the file names of the images that are included in the app bundle are (sort of) standardized and may not be related to what you've named the original files.
In particular, when you use the LaunchImage image set, the files that end up in the application bundle have names like
- LaunchImage.png
- [email protected]
- [email protected]
- [email protected]
- [email protected]
- LaunchImage-700-Landscape@2x~ipad.png
etc. Note, in particular, they are not named Default.png
or any variation of that. Even if that's what you called the files. Once you've dropped them in one of the wells in the asset catalog, they come out the other end with a standard name.
So [UIImage imageNamed:@"Default"]
won't work because there is no such file in the app bundle. However, [UIImage imageNamed:@"LaunchImage"]
will work (assuming you've filled either the iPhone Portrait 2x well or the pre iOS7 iPhone Portrait 1x well).
The documentation indicates that the imageNamed:
method on UIImage
should auto-magically select the correct version, but I think this only applies to image sets other than the launch image--at least I've not gotten it to work quite correctly (could just be me not doing something right).
So depending on your exact circumstances, you might need to do a little trial and error to get the correct file name. Build and run the app in the simulator and then you can always look in the appropriate subdirectory of ~/Library/Application Support/iPhone Simulator
to verify what the actual file names in the app bundle are.
But again, the main point is that there is no need to include duplicates of the image files and you don't need to make any adjustments to the Copy Bundle Resources
build phase.