//1.创建按钮
UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(100, 200, 50, 50)];
[button setTitle:@"截屏" forState:UIControlStateNormal];
[button addTarget:self action:@selector(tap:) forControlEvents:UIControlEventTouchUpInside];
button.backgroundColor = [UIColor redColor];
[self.view addSubview:button];
//2.按钮点击事件
- (void)tap:(UIButton *)sender{
// 1.截取全屏
//上下文大小
UIGraphicsBeginImageContext(self.view.bounds.size);
//获取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
//渲染 实时绘图
[self.view.layer renderInContext:context];
//取出图片
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
// 把图片保存到相册
UIImageWriteToSavedPhotosAlbum(image,nil, nil, nil);
}
效果图