1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. //1.开启上下文 UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0.0); //2.获取上下文 CGContextRef ctx=UIGraphicsGetCurrentContext(); //3.渲染控制器view的图层到上下文,图层只能渲染不能用draw [self.view.layer renderInContext:ctx]; //4.获取截屏图片 UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext(); //5.关闭上下文 UIGraphicsEndImageContext(); //6.将截图保存到桌面 NSData *data=UIImagePNGRepresentation(newImage); [data writeToFile:@"/Users/zxc/Desktop/cut.png" atomically:YES]; }
|