【问题标题】:How do I capture the entire HTML page (including overflow) in a UIWebView?如何在 UIWebView 中捕获整个 HTML 页面(包括溢出)?
【发布时间】:2015-03-01 04:08:42
【问题描述】:

下面的代码捕获了 web 视图的屏幕而不是整个页面:

func screenShotMethod() {
    //Create the UIImage
    UIGraphicsBeginImageContext(view.frame.size)
    view.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

    //Save it to the camera roll
    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}

如何在 UIWebView 中捕获整个 HTML 页面(包括溢出)?

【问题讨论】:

  • 让 webview 和 context 的高度都变大。
  • 我是新手。请更具体。

标签: swift uiwebview xcode6.1


【解决方案1】:

我知道已经很晚了,但这个可以帮助别人:

let webViewFrame = uiWebViewObj.frame
let scrollViewOffset = uiWebViewObj.scrollView.contentOffset
if webView.scrollView.contentSize.width == 0 || webView.scrollView.contentSize.height == 0{
  // nothing to take screenshot of 
}
//Set the scrollView offset to top of the web Page
uiWebViewObj.scrollView.setContentOffset(CGPointMake(0, -webView.scrollView.contentInset.top), animated: false)
let screenRect =  UIScreen.mainScreen().bounds
uiWebViewObj.frame = CGRectMake(webViewFrame.origin.x, webViewFrame.origin.y, screenRect.size.width, uiWebViewObj.scrollView.contentSize.height)
 let cgSizeToUse = CGSizeMake(screenRect.size.width, uiWebViewObj.scrollView.contentSize.height)
autoreleasepool{
    UIGraphicsBeginImageContextWithOptions(cgSizeToUse, false, 0) 
    uiWebViewObj.layer.renderInContext(UIGraphicsGetCurrentContext())
    let image = UIGraphicsGetImageFromCurrentContext()
    UIGraphicsEndImageContext()
}

uiWebViewObj.frame = webViewFrame
uiWebObj.scrollView.contentOffset = scrollViewOffset

参考链接: http://swiftgazelle.com/2015/08/how-to-take-uiwebview-screenshot-in-swift-on-ios/

【讨论】:

    猜你喜欢
    • 2017-01-28
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2012-04-10
    • 1970-01-01
    • 2011-10-07
    相关资源
    最近更新 更多