美文网首页
截屏分享UIView生成的图片包含windows和keywind

截屏分享UIView生成的图片包含windows和keywind

作者: NB的开发 | 来源:发表于2021-09-02 17:24 被阅读0次

    - (NSData *)imageDataScreenShot{

        CGSizeimageSize = [UIScreenmainScreen].bounds.size;

       UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);

        CGContextRef context = UIGraphicsGetCurrentContext();

        for(UIWindow*windowin[[UIApplicationsharedApplication]windows]) {

            CGContextSaveGState(context);

            CGContextTranslateCTM(context, window.center.x, window.center.y);

            CGContextConcatCTM(context, window.transform);

            CGContextTranslateCTM(context, -window.bounds.size.width * window.layer.anchorPoint.x, -window.bounds.size.height * window.layer.anchorPoint.y);

            if([windowrespondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {

                [windowdrawViewHierarchyInRect:window.bounds afterScreenUpdates:YES];

            }else{

                [window.layerrenderInContext:context];

            }

            CGContextRestoreGState(context);

        }

        CGContextSaveGState(context);

        CGContextTranslateCTM(context, [UIApplication sharedApplication].keyWindow.center.x, [UIApplication sharedApplication].keyWindow.center.y);

        CGContextConcatCTM(context, [UIApplication sharedApplication].keyWindow.transform);

        CGContextTranslateCTM(context, -[UIApplication sharedApplication].keyWindow.bounds.size.width * [UIApplication sharedApplication].keyWindow.layer.anchorPoint.x, -[UIApplication sharedApplication].keyWindow.bounds.size.height * [UIApplication sharedApplication].keyWindow.layer.anchorPoint.y);

        if([[UIApplicationsharedApplication].keyWindowrespondsToSelector:@selector(drawViewHierarchyInRect:afterScreenUpdates:)]) {

            [[UIApplication sharedApplication].keyWindow drawViewHierarchyInRect:[UIApplication sharedApplication].keyWindow.bounds afterScreenUpdates:YES];

        }else{

            [[UIApplication sharedApplication].keyWindow.layer renderInContext:context];

        }

        CGContextRestoreGState(context);

        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

        return UIImagePNGRepresentation(image);

    }

    相关文章

      网友评论

          本文标题:截屏分享UIView生成的图片包含windows和keywind

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