保存完整的截图,包括栏

时间:2013-04-26 17:54:47

标签: ios objective-c xcode

我遇到以下代码的问题只拍了我的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;
}

2 个答案:

答案 0 :(得分:12)

几个月前following代码对我有用:

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

[layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();

答案 1 :(得分: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!
    }