从Images.xcassets获取默认启动图LaunchImage(更新至iPhoneX)

在开发中,需要用代码获取默认启动图。
 UIImageView  *customLoadingView = [[UIImageView alloc] initWithFrame:self.window.bounds];
    if (iPhone5) {
        customLoadingView.image = [UIImage imageNamed:@"LaunchImage-700-568h"];
    }else if (iPhone6) {
        customLoadingView.image = [UIImage imageNamed:@"LaunchImage-800-667h"];
    }else if (iPhone6P) {
        customLoadingView.image = [UIImage imageNamed:@"LaunchImage-800-Portrait-736h"];
    }else if (iPhoneX) {
        customLoadingView.image = [UIImage imageNamed:@"LaunchImage-1100-Portrait-2436h"];
    } else {
        customLoadingView.image = [UIImage imageNamed:@"LaunchImage-700"];
    }

[self.window addSubview:customLoadingView];
打印app里面所有启动图片名称信息
/** 打印app里面所有启动图片名称信息 */
- (void)printAllLaunchImageInfo{
    
    NSDictionary *infoDict = [[NSBundle mainBundle] infoDictionary];
    
    //获取所有启动图片信息数组
    NSArray *launchImagesArr = infoDict[@"UILaunchImages"];
    
    NSLog(@"launchImagesArr: %@", launchImagesArr);
打印日志:启动图片的名字是固定的
    /* 打印日志:启动图片的名字是固定的*/
    launchImagesArr: (
        {
        UILaunchImageMinimumOSVersion = "11.0";
        UILaunchImageName = "LaunchImage-1100-Portrait-2436h";
        UILaunchImageOrientation = Portrait;
        UILaunchImageSize = "{375, 812}";
    },
        {
        UILaunchImageMinimumOSVersion = "8.0";
        UILaunchImageName = "LaunchImage-800-Portrait-736h";
        UILaunchImageOrientation = Portrait;
        UILaunchImageSize = "{414, 736}";
    },
        {
        UILaunchImageMinimumOSVersion = "8.0";
        UILaunchImageName = "LaunchImage-800-667h";
        UILaunchImageOrientation = Portrait;
        UILaunchImageSize = "{375, 667}";
    },
        {
        UILaunchImageMinimumOSVersion = "7.0";
        UILaunchImageName = "LaunchImage-700";
        UILaunchImageOrientation = Portrait;
        UILaunchImageSize = "{320, 480}";
    },
        {
        UILaunchImageMinimumOSVersion = "7.0";
        UILaunchImageName = "LaunchImage-700-568h";
        UILaunchImageOrientation = Portrait;
        UILaunchImageSize = "{320, 568}";
    }
)
     */
}

从打印的 UILaunchImageName 可以看出名字的构造规律:
"LaunchImage-{UILaunchImageMinimumOSVersion * 100}-{ UILaunchImageOrientation }-{高度(iPhoneX是用2436h,其实是对应的点)}"

虽然有这个规律,但具体还是得看打印出的名字(UILaunchImageName)

PS:如果你程序兼容7.0以下,那么你就需要判断版本,因为在ios6上获取启动图是@“LaunchImage”


参考文章:
1.从Images.xcassets获取默认启动图LaunchImage
2.iOS获取app图标和启动图片名字(AppIcon and LaunchImage's name)(一)

你可能感兴趣的:(从Images.xcassets获取默认启动图LaunchImage(更新至iPhoneX))