IT虾米网

IOS截取屏幕到文件中

wyy 2018年06月23日 手机开发 1075 0

在这里首先明确,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.   
    }  

评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!