WKWebView 整个网页截屏

坑1. WKWebView 截屏问题

在iOS8之前,使用的UIWebView是renderInContext 截图.

- (UIImage *)screenView:(UIScrollView *)view andOriginFrame:(CGRect)originFrame{
    
    UIGraphicsBeginImageContextWithOptions(CGSizeMake(SCREEN_WIDTH,view.frame.size.height), YES, 0);     //设置截屏大小
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGImageRef imageRef = viewImage.CGImage;
    UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRef];
    UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库
    return sendImage;
    
}

但是,直接UIWebView替换成WKWebView,会发现截屏的出来的Image 是 WKWebView的背景色,里面没有任何内容.

- (UIImage*)captureView:(UIView *)theView frame:(CGRect)originFrame
{
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage *img;
    if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
    {
        for(UIView *subview in theView.subviews)
        {
            [subview drawViewHierarchyInRect:subview.bounds afterScreenUpdates:YES];
        }
        img = UIGraphicsGetImageFromCurrentImageContext();
    }
    else
    {
        CGContextSaveGState(context);
        [theView.layer renderInContext:context];
        img = UIGraphicsGetImageFromCurrentImageContext();
    }
    UIGraphicsEndImageContext();
    CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, theView.frame);
    UIImage *CGImg = [UIImage imageWithCGImage:ref];
    CGImageRelease(ref);
    
    //图行复原
    theView.frame = originFrame;
    [theView.superview layoutIfNeeded];
    
    return CGImg;
}

替换成上面的方法,将renderInContext: 替换成drawViewHierarchyInRect:afterScreenUpdates:截屏正常

坑2.截取整个界面

具体方法如下,实现逻辑是:

  • 将WKWebView的ScrollView设置为跟WKWebView 内容一般大小.这样ScrollView 现在就能展示整个的WebView(在屏幕外面的内容看不到而已).
  • 然后将scrollView内的全部图层绘制到上下文中.
  • 保存图片
-(UIImage *)getCoverView{
    
    UIScrollView *rt = self.view.subviews.firstObject;
    rt.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64);
    CGRect originFrame = rt.frame;
    CGRect frm=rt.frame;
    frm.size.height = self.wkWebView.scrollView.contentSize.height;
    rt.frame=frm;
    [rt.superview layoutIfNeeded];
    return  [self  captureView:rt frame:originFrame];
   
}
- (UIImage*)captureView:(UIView *)theView frame:(CGRect)originFrame
{
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    UIImage *img;
    if([[[UIDevice currentDevice] systemVersion] floatValue]>=7.0)
    {
        for(UIView *subview in theView.subviews)
        {
            [subview drawViewHierarchyInRect:subview.bounds afterScreenUpdates:YES];
        }
        img = UIGraphicsGetImageFromCurrentImageContext();
    }
    else
    {
        CGContextSaveGState(context);
        [theView.layer renderInContext:context];
        img = UIGraphicsGetImageFromCurrentImageContext();
    }
    UIGraphicsEndImageContext();
    CGImageRef ref = CGImageCreateWithImageInRect(img.CGImage, theView.frame);
    UIImage *CGImg = [UIImage imageWithCGImage:ref];
    CGImageRelease(ref);
    
    //图行复原
    theView.frame = originFrame;
    [theView.superview layoutIfNeeded];
    
    return CGImg;
}

你可能感兴趣的:(WKWebView 整个网页截屏)