在这里首先明确,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. }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/3310.html