美文网首页
webView全部内容截取成图片

webView全部内容截取成图片

作者: DanDing | 来源:发表于2016-10-18 13:44 被阅读28次
        UIGraphicsBeginImageContext(webView.bounds.size); 
    [webView.layer renderInContext:UIGraphicsGetCurrentContext()];
        
        _currentImage = [self getImageFromView:webView];
        UIGraphicsEndImageContext();
        UIImageWriteToSavedPhotosAlbum(_currentImage, self, @selector(imageSavedToPhotosAlbum:didFinishSavingWithError:contextInfo:), nil); 
    - (UIImage *)getImageFromView:(UIView *)view
    {
        UIImage *pngImg;   
        UIGraphicsBeginImageContextWithOptions(CGSizeMake(viewSize.width, viewSize.height),YES,[UIScreen mainScreen].scale);
        // 设置view成全部展开效果
        [view setFrame: CGRectMake(0, 0, view.size.width, view.size.height)];
        CGContextRef context = UIGraphicsGetCurrentContext();
        [view.layer renderInContext:context];
        pngImg =  UIGraphicsGetImageFromCurrentImageContext() ;
        UIGraphicsEndImageContext();
        return pngImg;
    }
    - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
    {
        NSString *message = @"text";
        if (!error) {
            message = @"成功保存到相册";
            
        }else
        {
            message = [error description];
        }
    } 
    

    注意:scale(缩放因子)大小影响截图内容清晰度,尽量写[UIScreen mainScreen].scale

    相关文章

      网友评论

          本文标题:webView全部内容截取成图片

          本文链接:https://www.haomeiwen.com/subject/ruqtuttx.html