思路:
- 一般是将整个屏幕截取下来
- 创建上下文时,上下文的范围就是控制器View的大小
- 对当前view进行绘制,其实就是把View上的layer渲染到上下文中
- (UIImage *)imageWithCaputureView:(UIView *)view
{
//创建位图上下文
UIGraphicsBeginImageContextWithOptions(self.view.bounds.size, NO, 0);
//获取当前上下文
CGContextRef ctx = UIGraphicsGetCurrentContext();
//把view的layer层渲染到上下文,layer只能渲染不能绘制,所以在此前需要获取当前上下文
[self.view.layer renderInContext:ctx];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
//关闭上下文
UIGraphicsEndImageContext();
return image;
}
最后,附上相关的demo,Git:(https://github.com/hejiasu/Drawing)