Xcode 5 & Asset Catalog: How to reference the LaunchImage?

前端 未结 14 1494
萌比男神i
萌比男神i 2020-11-28 18:17

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

相关标签:
14条回答
  • 2020-11-28 18:42

    One can easily access Launch image by one line of code.

     UIImage *myAppsLaunchImage = [UIImage launchImage];
    

    Please follow steps given below to achieve functionality depicted above.

    Step 1. Extend UIImage class by creating a category & add following method to it.

    + (UIImage *)launchImage {
        NSDictionary *dOfLaunchImage = [NSDictionary dictionaryWithObjectsAndKeys:
                                        @"LaunchImage-568h@2x.png",@"568,320,2,8,p", // ios 8 - iphone 5 - portrait
                                        @"LaunchImage-568h@2x.png",@"568,320,2,8,l", // ios 8 - iphone 5 - landscape
                                        @"LaunchImage-700-568h@2x.png",@"568,320,2,7,p", // ios 7 - iphone 5 - portrait
                                        @"LaunchImage-700-568h@2x.png",@"568,320,2,7,l", // ios 7 - iphone 5 - landscape
                                        @"LaunchImage-700-Landscape@2x~ipad.png",@"1024,768,2,7,l", // ios 7 - ipad retina - landscape
                                        @"LaunchImage-700-Landscape~ipad.png",@"1024,768,1,7,l", // ios 7 - ipad regular - landscape
                                        @"LaunchImage-700-Portrait@2x~ipad.png",@"1024,768,2,7,p", // ios 7 - ipad retina - portrait
                                        @"LaunchImage-700-Portrait~ipad.png",@"1024,768,1,7,p", // ios 7 - ipad regular - portrait
                                        @"LaunchImage-700@2x.png",@"480,320,2,7,p", // ios 7 - iphone 4/4s retina - portrait
                                        @"LaunchImage-700@2x.png",@"480,320,2,7,l", // ios 7 - iphone 4/4s retina - landscape
                                        @"LaunchImage-Landscape@2x~ipad.png",@"1024,768,2,8,l", // ios 8 - ipad retina - landscape
                                        @"LaunchImage-Landscape~ipad.png",@"1024,768,1,8,l", // ios 8 - ipad regular - landscape
                                        @"LaunchImage-Portrait@2x~ipad.png",@"1024,768,2,8,p", // ios 8 - ipad retina - portrait
                                        @"LaunchImage-Portrait~ipad.png",@"1024,768,1,8,l", // ios 8 - ipad regular - portrait
                                        @"LaunchImage.png",@"480,320,1,7,p", // ios 6 - iphone 3g/3gs - portrait
                                        @"LaunchImage.png",@"480,320,1,7,l", // ios 6 - iphone 3g/3gs - landscape
                                        @"LaunchImage@2x.png",@"480,320,2,8,p", // ios 6,7,8 - iphone 4/4s - portrait
                                        @"LaunchImage@2x.png",@"480,320,2,8,l", // ios 6,7,8 - iphone 4/4s - landscape
                                        @"LaunchImage-800-667h@2x.png",@"667,375,2,8,p", // ios 8 - iphone 6 - portrait
                                        @"LaunchImage-800-667h@2x.png",@"667,375,2,8,l", // ios 8 - iphone 6 - landscape
                                        @"LaunchImage-800-Portrait-736h@3x.png",@"736,414,3,8,p", // ios 8 - iphone 6 plus - portrait
                                        @"LaunchImage-800-Landscape-736h@3x.png",@"736,414,3,8,l", // ios 8 - iphone 6 plus - landscape
                                        nil];
        NSInteger width = ([UIScreen mainScreen].bounds.size.width>[UIScreen mainScreen].bounds.size.height)?[UIScreen mainScreen].bounds.size.width:[UIScreen mainScreen].bounds.size.height;
        NSInteger height = ([UIScreen mainScreen].bounds.size.width>[UIScreen mainScreen].bounds.size.height)?[UIScreen mainScreen].bounds.size.height:[UIScreen mainScreen].bounds.size.width;
        NSInteger os = [[[[[UIDevice currentDevice] systemVersion] componentsSeparatedByString:@"."] objectAtIndex:0] integerValue];
        NSString *strOrientation = UIDeviceOrientationIsLandscape([[UIDevice currentDevice] orientation])?@"l":@"p";
        NSString *strImageName = [NSString stringWithFormat:@"%li,%li,%li,%li,%@",width,height,(NSInteger)[UIScreen mainScreen].scale,os,strOrientation];
        UIImage *imageToReturn = [UIImage imageNamed:[dOfLaunchImage valueForKey:strImageName]];
        if([strOrientation isEqualToString:@"l"] && [strImageName rangeOfString:@"Landscape"].length==0) {
            imageToReturn = [UIImage rotate:imageToReturn orientation:UIImageOrientationRight];
        }
        return imageToReturn;
    }
    

    Step 2. Above method should be working by adding following code also into same category of UIImage

    static inline double radians (double degrees) {return degrees * M_PI/180;}
    
    + (UIImage *)rotate:(UIImage*)src orientation:(UIImageOrientation) orientation {
        UIGraphicsBeginImageContext(src.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        if (orientation == UIImageOrientationRight) {
            CGContextRotateCTM (context, radians(90));
        } else if (orientation == UIImageOrientationLeft) {
            CGContextRotateCTM (context, radians(-90));
        } else if (orientation == UIImageOrientationDown) {
            // NOTHING
        } else if (orientation == UIImageOrientationUp) {
            CGContextRotateCTM (context, radians(90));
        }
        [src drawAtPoint:CGPointMake(0, 0)];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return image;
    }
    
    0 讨论(0)
  • 2020-11-28 18:46

    The LaunchImages are special, and aren't actually an asset catalog on the device. If you look using iFunBox/iExplorer/etc (or on the simulator, or in the build directory) you can see the final names, and then write code to use them - eg. for an iOS7-only iPhone-only project, this will set the right launch image:

    NSString *launchImage;
    if  ((UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) &&
         ([UIScreen mainScreen].bounds.size.height > 480.0f)) {
        launchImage = @"LaunchImage-700-568h";
    } else {
        launchImage = @"LaunchImage-700";
    }
    
    [self.launchImageView setImage:[UIImage imageNamed:launchImage]];
    

    I put this into viewDidLoad.

    This isn't really ideal, it would be great if Apple would give us a nice API to do this.

    0 讨论(0)
  • 2020-11-28 18:47

    @codeman's answer updated for Swift 1.2:

    func splashImageForOrientation(orientation: UIInterfaceOrientation, size: CGSize) -> String? {
        var viewSize        = size
        var viewOrientation = "Portrait"
    
        if UIInterfaceOrientationIsLandscape(orientation) {
            viewSize        = CGSizeMake(size.height, size.width)
            viewOrientation = "Landscape"
        }
    
        if let imagesDict = NSBundle.mainBundle().infoDictionary as? [String: AnyObject] {
            if let imagesArray = imagesDict["UILaunchImages"] as? [[String: String]] {
                for dict in imagesArray {
                    if let sizeString = dict["UILaunchImageSize"], let imageOrientation = dict["UILaunchImageOrientation"] {
                        let imageSize = CGSizeFromString(sizeString)
                        if CGSizeEqualToSize(imageSize, viewSize) && viewOrientation == imageOrientation {
                            if let imageName = dict["UILaunchImageName"] {
                                return imageName
                            }
                        }
                    }
                }
            }
        }
    
        return nil
    
    }
    

    To call it, and to support rotation for iOS 8:

    override func viewWillAppear(animated: Bool) {
        if let img = splashImageForOrientation(UIApplication.sharedApplication().statusBarOrientation, size: self.view.bounds.size) {
            backgroundImage.image = UIImage(named: img)
        }
    }
    
    override func viewWillTransitionToSize(size: CGSize, withTransitionCoordinator coordinator: UIViewControllerTransitionCoordinator) {
        let orientation = size.height > size.width ? UIInterfaceOrientation.Portrait : UIInterfaceOrientation.LandscapeLeft
    
        if let img = splashImageForOrientation(orientation, size: size) {
            backgroundImage.image = UIImage(named: img)
        }
    
    }
    

    Just what I needed, thanks!

    0 讨论(0)
  • 2020-11-28 18:50

    My app currently only supports iOS 7 and later.

    This is how I reference the launch image from the asset catalog:

    NSDictionary *dict = @{@"320x480" : @"LaunchImage-700",
                           @"320x568" : @"LaunchImage-700-568h",
                           @"375x667" : @"LaunchImage-800-667h",
                           @"414x736" : @"LaunchImage-800-Portrait-736h"};
    NSString *key = [NSString stringWithFormat:@"%dx%d",
        (int)[UIScreen mainScreen].bounds.size.width,
        (int)[UIScreen mainScreen].bounds.size.height];
    UIImage *launchImage = [UIImage imageNamed:dict[key]];
    

    You can add more key value pairs if you want to support older iOS versions.

    0 讨论(0)
  • 2020-11-28 18:51
    - (NSString *)splashImageNameForOrientation:(UIInterfaceOrientation)orientation {
        CGSize viewSize = self.view.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;
    }
    
    0 讨论(0)
  • 2020-11-28 18:51

    Updated to latest Swift syntax (Swift 5)

       func splashImageForOrientation(orientation: UIInterfaceOrientation) -> String? {
    
        var viewSize = screenSize
        var viewOrientation = "Portrait"
        if orientation.isLandscape {
            viewSize = CGSize(width: viewSize.height, height: viewSize.width)
            viewOrientation = "Landscape"
        }
        if let infoDict = Bundle.main.infoDictionary, let launchImagesArray = infoDict["UILaunchImages"] as? [Any] {
            for launchImage in launchImagesArray {
                if let launchImage = launchImage as? [String: Any], let nameString = launchImage["UILaunchImageName"] as? String, let sizeString = launchImage["UILaunchImageSize"] as? String, let orientationString = launchImage["UILaunchImageOrientation"] as? String {
                    let imageSize = NSCoder.cgSize(for: sizeString)
                    if imageSize.equalTo(viewSize) && viewOrientation == orientationString {
                        return nameString
                    }
                }
            }
        }
        return nil
    }
    
    0 讨论(0)
提交回复
热议问题