【问题标题】:converting html to image in ios在ios中将html转换为图像
【发布时间】:2013-01-03 04:03:31
【问题描述】:

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

【问题讨论】:

    标签: iphone html objective-c ios ipad


    【解决方案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
    }
    

    【讨论】:

    • 我在这里得到一个空白图像??
    • 这对我有用。您确定您的 Web 视图已完成加载,并且在缩小时不会变成黑色图像?
    【解决方案2】:

    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)
                    }
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-01
      • 2011-06-02
      • 2010-10-14
      • 1970-01-01
      • 2018-12-05
      • 1970-01-01
      • 2016-02-25
      • 2019-04-30
      相关资源
      最近更新 更多