直接上代码(有注意点):
- (UIImage *)captureScrollView:(UIScrollView *)scrollView{
UIImage* image = nil;
// UIGraphicsBeginImageContext(scrollView.contentSize);
// 注意: 1. 第一个参数 是 滚动区域, 2. 第二个参数 如果是yes 则会变黑色
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 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;
}
网友评论