- (UIImage*)captureScrollView:(UIScrollView*)scrollView{
之前有过scrollView 截取长图的需求从网上搜索的是以下代码
UIImage* image = nil;
UIGraphicsBeginImageContext(scrollView.contentSize);
{
CGPoint savedContentOffset = scrollView.contentOffset;
CGRect savedFrame = scrollView.frame;
UIGraphicsBeginImageContextWithOptions(scrollView.frame.size,NO, [UIScreen mainScreen].scale);
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) {
returnimage;
}
}
但是最近iOS13测试后的时候发现长图显示不全,经过查询以下代码可解决
- (UIImage*)captureScrollView:(UIScrollView*)scrollView{
UIImage* viewImage = nil;
UITableView *scrollView = tableview;
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, NO, 0.0);
{
// 保存原来的偏移量 CGPoint savedContentOffset = scrollView.contentOffset;
// CGPoint savedFrame = scrollView.frame; // 设置截图需要的偏移量和frame scrollView.contentOffset = CGPointZero;
scrollView.frame = CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height);
// 创建临时view,并且把要截图的view添加到临时view上面 UIView *tempView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, scrollView.contentSize.width, scrollView.contentSize.height)];
[scrollView removeFromSuperview];
[tempView addSubview:scrollView];
// 对临时view进行截图 [tempView.layer renderInContext:UIGraphicsGetCurrentContext()];
viewImage = UIGraphicsGetImageFromCurrentImageContext();
// 恢复截图view原来的状态 [scrollView removeFromSuperview];
[self addSubview:scrollView];
scrollView.contentOffset = savedContentOffset;
// 如果原来是frame布局,需要设置frame,如果是Auto layout需要再次进行Auto layout布局。 // scrollView.frame = savedFrame; [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.edges.equalTo(self);
}];
}
UIGraphicsEndImageContext();
return viewImage;
}
return viewImage;