6

我在使用以下代码时遇到问题,仅拍摄我的 UITableView 的照片,而不是带有导航和标签栏的整个屏幕...

我想要全屏捕获,包括导航和标签栏。非常感谢任何帮助。

- (UIImage*)captureView:(UIView *)view
{
CGRect rect = [[UIScreen mainScreen] bounds];
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[view.layer renderInContext:context];
UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return img;
}
4

2 回答 2

12

几个月前,以下代码对我有用

CALayer *layer = [[UIApplication sharedApplication] keyWindow].layer;
CGFloat scale = [UIScreen mainScreen].scale;
UIGraphicsBeginImageContextWithOptions(layer.frame.size, NO, scale);

[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
于 2013-04-26T18:47:17.760 回答
1

Swift 3.0 版本,如果有人需要

func getScreenShot() -> UIImage {

        let layer : CALayer = (UIApplication.shared.keyWindow?.layer)!
        let scale : CGFloat = UIScreen.main.scale
        UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale)
        layer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
于 2017-06-19T06:22:00.107 回答