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

- (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和keywindow)