【发布时间】:2013-01-03 04:03:31
【问题描述】:
我在我的应用程序中使用多个 .html 文件并在 webview 中打开它,我还需要将 html 页面显示为缩略图视图,是否可以将 html 页面转换为图像或任何其他方法来显示html 文件作为预览。有知道的请帮帮我
【问题讨论】:
标签: iphone html objective-c ios ipad
我在我的应用程序中使用多个 .html 文件并在 webview 中打开它,我还需要将 html 页面显示为缩略图视图,是否可以将 html 页面转换为图像或任何其他方法来显示html 文件作为预览。有知道的请帮帮我
【问题讨论】:
标签: iphone html objective-c ios ipad
-(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
}
【讨论】:
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)
}
}
}
}
}
【讨论】: