看了好多文章都没有好的办法,自己改了一个方法,达到效果,大家可以尝试.好用的请给个赞
- (UIImage*)captureView:(WKWebView *)webView
{
UIImage* image = nil;
//优化图片截取不清晰
UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize, true, [UIScreen mainScreen].scale);
{
CGPoint savedContentOffset = webView.scrollView.contentOffset;
CGRect savedFrame = webView.scrollView.frame;
webView.scrollView.contentOffset = CGPointZero;
webView.scrollView.frame = CGRectMake(0, 0, webView.scrollView.contentSize.width, webView.scrollView.contentSize.height);
for (UIView * subView in webView.subviews) {
[subView drawViewHierarchyInRect:subView.bounds afterScreenUpdates:YES];
}
image = UIGraphicsGetImageFromCurrentImageContext();
webView.scrollView.contentOffset = savedContentOffset;
webView.scrollView.frame = savedFrame;
}
UIGraphicsEndImageContext();
if (image != nil) {
return image;
}
return nil;
}