美文网首页iOS
iOS获取LaunchImage图片,解决iPhone XS M

iOS获取LaunchImage图片,解决iPhone XS M

作者: 回眸月 | 来源:发表于2019-03-19 17:43 被阅读0次

    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;
    }
    

    相关文章

      网友评论

        本文标题:iOS获取LaunchImage图片,解决iPhone XS M

        本文链接:https://www.haomeiwen.com/subject/bmunmqtx.html