美文网首页
iOS截取LaunchScreen.storyboard图片

iOS截取LaunchScreen.storyboard图片

作者: 小海豚丶 | 来源:发表于2022-01-21 16:03 被阅读0次
注意:控件不要使用SafeArea约束,不然获取的快照内容不准确

Swift

    private func launchScreenSnapshort() -> UIImage? {
        guard let info_dict = Bundle.main.infoDictionary,
              let storyboard_name = info_dict["UILaunchStoryboardName"] as? String,
              let storyboard_vc = UIStoryboard.init(name: storyboard_name, bundle: nil).instantiateInitialViewController(),
              let storyboard_view = storyboard_vc.view else {
                  return nil
              }
        let size = storyboard_view.bounds.size
        UIGraphicsBeginImageContextWithOptions(size, false, UIScreen.main.scale)
        let ctx = UIGraphicsGetCurrentContext()
        storyboard_view.layer.render(in: ctx!)
        let lauch_image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return lauch_image
    }

Objective-C

- (UIImage *)imageFromLaunchScreen {
    NSString *UILaunchStoryboardName = [[[NSBundle mainBundle] infoDictionary] valueForKey:@"UILaunchStoryboardName"];
    if (UILaunchStoryboardName == nil) {
        NSLog(@"从 LaunchScreen 中获取启动图失败!");
        return nil;
    }
    UIViewController *LaunchScreenSb = [[UIStoryboard storyboardWithName:UILaunchStoryboardName bundle:nil] instantiateInitialViewController];
    if (LaunchScreenSb) {
        UIView * view = LaunchScreenSb.view;
        // 加入到UIWindow后,LaunchScreenSb.view的safeAreaInsets在刘海屏机型才正常。
        UIWindow *containerWindow = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
        view.frame = containerWindow.bounds;
        [containerWindow addSubview:view];
        [containerWindow layoutIfNeeded];
        UIImage *image = [self imageFromView:view];
        containerWindow = nil;
        return image;
    }
    NSLog(@"从 LaunchScreen 中获取启动图失败!");
    return nil;
}

-(UIImage *)imageFromView:(UIView*)view {
    //fix bug:https://github.com/CoderZhuXH/XHLaunchAd/issues/203
    if (CGRectIsEmpty(view.frame)) {
        return nil;
    }
    CGSize size = view.bounds.size;
    //参数1:表示区域大小 参数2:如果需要显示半透明效果,需要传NO,否则传YES 参数3:屏幕密度
    UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
    if ([view respondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {
        [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
    }else{
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

相关文章

  • iOS截取LaunchScreen.storyboard图片

    代码 注意事项 storyboard的背景图片设置上下约束的时候的对象是superView,而不是Top Layo...

  • iOS截取LaunchScreen.storyboard图片

    注意:控件不要使用SafeArea约束,不然获取的快照内容不准确 Swift Objective-C

  • iOS 截取tableView, scrollerView图片

    iOS 截取tableView, scrollerView图片

  • iOS 如何使用  LaunchScreen.storyboar

    iOS 如何使用 LaunchScreen.storyboard 设 置启动图片(闪屏图)[https://we...

  • 裁剪图片的特定区域

    使用 封装 参考: iOS 截取图片 部分 并生成新图片

  • iOS13 设置启动图

    iOS13之后,启动图只能用LaunchScreen.storyboard设置,并且图片直接拖到assets中,不...

  • ios截取图片

    最近有朋友遇到要截取图片中间部分展示在imageView上的问题,想着社交应用还是很可能遇到这种情况的就写下来。 ...

  • iOS 图片截取

    业务逻辑:为需要切图的view添加pan手势,定义一个需要截取范围视图的成员属性,定义一个成员变量用来记录手指的开...

  • iOS 图片截取

    入参说明:image:原始图片,rect:需要截取的位置(范围 image.bound) 出参说明:大小为 rec...

  • iOS之图片截取

    在项目中有的时候需要用截取图片的功能,有的时候还需要将特定的视图进行截取,所以就写了这个demo。1.从相册中进行...

网友评论

      本文标题:iOS截取LaunchScreen.storyboard图片

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