iOS中保存全屏为图片(image)

保存全屏为图片

如果你需要生产一张屏幕截图为图片,下面的方法希望可以帮到你

CGSize imageSize = [[UIScreen mainScreen] bounds].size;
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext();
 
for (UIWindow * window in [[UIApplication sharedApplication] windows]) {
    if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {
        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);
        [[window layer] renderInContext:context];
 
        CGContextRestoreGState(context);
    }
}
 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

你可能感兴趣的:(iOS中保存全屏为图片(image))