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

时间:2015-03-01 04:08:42

标签: swift uiwebview xcode6.1

以下代码捕获屏幕,但不捕获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页面(包括溢出)?

1 个答案:

答案 0 :(得分:-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/