美文网首页
iOS中保存全屏为图片(image)

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

作者: zero_zql | 来源:发表于2016-05-07 12:17 被阅读144次

保存全屏为图片

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

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)

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