UIGraphicsBeginImageContext
首先说明一下UIGraphicsBeginImageContextWithOptions 和UIGraphicsBeginImageContext区别:
1: UIGraphicsBeginImageContext(CGSize size)
参数size为新创建的位图上下文的大小。它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小。
该函数的功能同UIGraphicsBeginImageContextWithOptions的功能相同,相当与UIGraphicsBeginImageContextWithOptions的opaque参数为NO,scale因子为1.0。
2 UIGraphicsBeginImageContextWithOptions
函数原型为:
void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
size——同UIGraphicsBeginImageContext
opaque—透明开关,如果图形完全不用透明,设置为YES以优化位图的存储。
scale—–缩放因子
特别说明,当缩放因子为0.0时候,图片不模糊
//截屏
- (UIImage *)CaptureScrollView:(UIScrollView *)scrollView{
UIImage* image = nil;
// UIGraphicsBeginImageContext(scrollView.contentSize); 默认
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 0.0);
{
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
[scrollView.layer renderInContext: UIGraphicsGetCurrentContext()];
image = UIGraphicsGetImageFromCurrentImageContext();
scrollView.contentOffset = savedContentOffset;
scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext();
if (image != nil) {
return image;
}
return nil;
}