I am using Xcode 5\'s Asset Catalog, and I would like to use my LaunchImage
as the background image of my home view (a pretty common practice to make the transi
Here a category on UIImage based on the solution provided by Cherpak Evgeny above.
UIImage+SplashImage.h:
#import <UIKit/UIKit.h>
/**
* Category on `UIImage` to access the splash image.
**/
@interface UIImage (SplashImage)
/**
* Return the name of the splash image for a given orientation.
* @param orientation The interface orientation.
* @return The name of the splash image.
**/
+ (NSString *)si_splashImageNameForOrientation:(UIInterfaceOrientation)orientation;
/**
* Returns the splash image for a given orientation.
* @param orientation The interface orientation.
* @return The splash image.
**/
+ (UIImage*)si_splashImageForOrientation:(UIInterfaceOrientation)orientation;
@end
UIImage+SplashImage.m:
#import "UIImage+SplashImage.h"
@implementation UIImage (SplashImage)
+ (NSString *)si_splashImageNameForOrientation:(UIInterfaceOrientation)orientation
{
CGSize viewSize = [UIScreen mainScreen].bounds.size;
NSString *viewOrientation = @"Portrait";
if (UIDeviceOrientationIsLandscape(orientation))
{
viewSize = CGSizeMake(viewSize.height, viewSize.width);
viewOrientation = @"Landscape";
}
NSArray* imagesDict = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchImages"];
for (NSDictionary *dict in imagesDict)
{
CGSize imageSize = CGSizeFromString(dict[@"UILaunchImageSize"]);
if (CGSizeEqualToSize(imageSize, viewSize) && [viewOrientation isEqualToString:dict[@"UILaunchImageOrientation"]])
return dict[@"UILaunchImageName"];
}
return nil;
}
+ (UIImage*)si_splashImageForOrientation:(UIInterfaceOrientation)orientation
{
NSString *imageName = [self si_splashImageNameForOrientation:orientation];
UIImage *image = [UIImage imageNamed:imageName];
return image;
}
@end
With help of Pichirichi's answer I've implemented the following category (iOS 7+) : UIImage+AssetLaunchImage
It's actually little more than generating name on the fly, but probably will be helpful.
In the documentation there is clearly stated:
"Each set in an asset catalog has a name. You can use that name to programmatically load any individual image contained in the set. To load an image, call the UIImage:ImageNamed: method, passing the name of the set that contains the image."
Using Pichirichi's list helps to solve this inconsistency.
Following @Pichirich's answer, I referenced my launchimage in InterfaceBuilder as:
"LaunchImage.png"
...and with Xcode 5.0.2, it's automatically plucking the appropriate image straight out of the Asset Catalog.
This is what I'd expect - except for Apple's viciously nasty move of silently renaming "Default.png" to "LaunchImage.png" :)
I realize that this is not necessarily the best solution for everyone but the easiest (and least error-prone, IMHO) way to do this is by making a separate entry in your Images.xcassets catalog. I called it SplashImage
.
When you go to add a new entry, make sure not to select "New Launch Image" as an option. Instead, select the generic "New Image Set". Next, open up the inspector and select the relevant options. If you're building for only retina devices, as I was, you can select the following:
This will leave you with four entries (iPhone 4S, iPhone 5(s,c), iPhone 6, and iPhone 6 Plus).
The files corresponding the the images are as follows:
| Resolution (Xcode entry) | Launch Image name | Device |
|--------------------------|---------------------|------------------|
| 1x | Default-750.png | iPhone 6 |
| 2x | Default@2x.png | iPhone 4S |
| Retina 4 2x | Default-568h@2x.png | iPhone 5, 5s, 5c |
| 3x | Default-1242.png | iPhone 6 Plus |
Of course, after you've done this you can simply use [UIImage imageNamed:@"SplashImage"]
Swift version of Cherpak Evgeny's answer:
func splashImageForOrientation(orientation: UIInterfaceOrientation) -> String {
var viewSize = self.view.bounds.size
var viewOrientation = "Portrait"
if UIInterfaceOrientationIsLandscape(orientation) {
viewSize = CGSizeMake(viewSize.height, viewSize.width)
viewOrientation = "Landscape"
}
let imagesDict = NSBundle.mainBundle().infoDictionary as Dictionary<NSObject,AnyObject>!
let imagesArray = imagesDict["UILaunchImages"] as NSArray
for dict in imagesArray {
let dictNSDict = dict as NSDictionary
let imageSize = CGSizeFromString(dictNSDict["UILaunchImageSize"] as String)
if CGSizeEqualToSize(imageSize, viewSize) && viewOrientation == (dictNSDict["UILaunchImageOrientation"] as String) {
return dictNSDict["UILaunchImageName"] as String
}
}
return ""
}