IOS截取屏幕到文件中

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

在这里首先明确,View中显示的东西都在在layer中的。

我们通过renderInContext来渲染layer中的内容

    - (void)viewDidLoad {  
        [super viewDidLoad];  
        //截取当前的View  
        //1.创建图层  
        UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);  
        //2.渲染layer  
        CGContextRef ctr = UIGraphicsGetCurrentContext();  
        [self.view.layer renderInContext:ctr];  
        CGContextStrokePath(ctr);  
        //3.取出image  
        UIImage *imageNew = UIGraphicsGetImageFromCurrentImageContext();  
        //4.关闭图层  
        UIGraphicsEndImageContext();  
          
        //5.将图片转换成NSData  
        NSData *data = UIImagePNGRepresentation(imageNew);  
        [data writeToFile:@"/Users/misaka/Desktop/1.png" atomically:YES];  
        // Do any additional setup after loading the view, typically from a nib.  
    }