iOS WebView生成长截图(截图分享功能实现)
产品经理今天提出一个新的需求,需要实现类似于每日优鲜、淘宝等客户端的截屏分享的功能,即点击分享按钮把整个H5报告(kwWebView展示的所有内容)截取成一个二进制流,再合成一张有大小的图片分享出去,如下效果:
1、给WKWebView添加类别 (WKWebView+ZFJViewCapture.h)
这个类别可以把网页全屏截图,直接上代码!
1. (void)ZFJContentCaptureCompletionHandler:(void(^)(UIImage *capturedImage))completionHandler{
CGPoint offset = self.scrollView.contentOffset;
UIView *snapShotView = [self snapshotViewAfterScreenUpdates:YES];
snapShotView.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, snapShotView.frame.size.width, snapShotView.frame.size.height);
[self.superview addSubview:snapShotView];
if(self.frame.size.height < self.scrollView.contentSize.height){
self.scrollView.contentOffset = CGPointMake(0, self.scrollView.contentSize.height - self.frame.size.height);
}
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.scrollView.contentOffset = CGPointZero;