1

我在我的应用程序中使用多个 .html 文件并在 webview 中打开它,我还需要将 html 页面显示为缩略图视图,是否可以将 html 页面转换为图像或任何其他方法来显示 html 文件作为预览。如果有人知道请帮助我

4

2 回答 2

1
-(void) webViewDidFinishLoad:(UIWebView *)webView { 

UIGraphicsBeginImageContext(CGSizeMake(webView.layer.frame.size.width, webView.layer.frame.size.height));
[webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


UIGraphicsBeginImageContext(CGSizeMake(70,100));
[image drawInRect:CGRectMake(0, 0, 70,100)];
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

//image is now a 70x100 thumbnail. make sure you include CoreGraphics
}
于 2013-01-03T04:05:14.347 回答
0

WKWebView 现在有一种更方便的方法

class WebviewDelegate: NSObject, WKNavigationDelegate {

    var completion: ((UIImage?) -> ())?

    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript("document.body.offsetHeight;") { (result, error) in
            if let height = result as? CGFloat {
                let config = WKSnapshotConfiguration()
                config.rect = CGRect(x: 0, y: 0, width: webView.scrollView.contentSize.width, height: height)

                webView.takeSnapshot(with: config) { (image, error) in
                    self.completion?(image)
                }
            }
        }
    }
}
于 2019-11-26T13:47:25.637 回答