Swift版本
func getLaunchImage() -> UIImage {
var lauchImg : UIImage!
var viewOrientation : String!
let viewSize = UIScreen.main.bounds.size
let scale = UIScreen.main.scale
let orientation = UIApplication.shared.statusBarOrientation
if orientation == .landscapeLeft || orientation == .landscapeRight {
viewOrientation = "Landscape"
} else {
viewOrientation = "Portrait"
}
let imgsInfoArray = Bundle.main.infoDictionary!["UILaunchImages"]
for dict : Dictionary <String, String> in imgsInfoArray as! Array {
let imageSize = NSCoder.cgSize(for: dict["UILaunchImageSize"]!)
if __CGSizeEqualToSize(imageSize, viewSize) && viewOrientation == dict["UILaunchImageOrientation"]! as String {
let img = UIImage(named: dict["UILaunchImageName"]!)
/*打印上面的启动页图片数组可以看到:
{
UILaunchImageMinimumOSVersion = "12.0";
UILaunchImageName = "LaunchImage-1200-Portrait-2688h";
UILaunchImageOrientation = Portrait;
UILaunchImageSize = "{414, 896}";
},
{
UILaunchImageMinimumOSVersion = "12.0";
UILaunchImageName = "LaunchImage-1200-Portrait-1792h";
UILaunchImageOrientation = Portrait;
UILaunchImageSize = "{414, 896}";
},
iPhone XS Max会取到iPhone XR的启动页,所以造成显示不正确,需加入下面的判断,oc代码一样,就不添加了
*/
if img.scale == scale {
launchImg = img
break
}
}
}
return lauchImg
}
Object-C版本,需加入上面注释部分的逻辑
- (UIImage *)getLaunchImage {
UIImage *launchImg = [[UIImage alloc] init];
NSString *orientation = @"";
CGSize screenSize = [UIScreen mainScreen].bounds.size;
UIInterfaceOrientation statusBarOrientation = [[UIApplication sharedApplication] statusBarOrientation];
if (statusBarOrientation == UIInterfaceOrientationLandscapeLeft || statusBarOrientation == UIInterfaceOrientationLandscapeRight) {
orientation = @"Landscape";
} else {
orientation = @"Portrait";
}
NSArray *imgsInfoArr = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"UILaunchImages"];
for (NSDictionary *info in imgsInfoArr) {
CGSize imgSize = CGSizeFromString(info[@"UILaunchImageSize"]);
if (CGSizeEqualToSize(imgSize, screenSize) && [orientation isEqualToString:info[@"UILaunchImageOrientation"]] ) {
launchImg = [UIImage imageNamed:info[@"UILaunchImageName"]];
}
}
return launchImg;
}
网友评论