鍍金池/ 問答/C  iOS/ ios WKWebView 網(wǎng)頁截長圖

ios WKWebView 網(wǎng)頁截長圖

怎樣截取網(wǎng)頁長圖,之前的 UIWebview 是可以用網(wǎng)上常用的方法,但是 WKWEbView 不能用了,截圖之后只有當(dāng)前顯示的部分,其他部分都是空白,沒有渲染上。求解,好人一生平安?。?!

回答
編輯回答
醉淸風(fēng)

https://github.com/dudongge/D... 已經(jīng)升級到swift 4.0 了 能解決你的問題。

2017年11月21日 19:35
編輯回答
情殺

WKWebView 你試試我寫的代碼, 原來使用 swift 2 寫的.

func convertWholePageToImage() -> UIImage{
    let boundsWidth = webView.bounds.width
    let boundsHeight = webView.bounds.height
    let scrollView = self.webView.scrollView
    let oldOffset = scrollView.contentOffset
    scrollView.setContentOffset(CGPointZero, animated: false)
    var contentHeight = scrollView.contentSize.height
    //        let screenHeight = self.webview.bounds.height
    let scale = UIScreen.mainScreen().scale
    
    var images:[UIImage] = []
    while contentHeight > 0{
      UIGraphicsBeginImageContextWithOptions(webView.bounds.size, false, scale)
      webView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
      let image = UIGraphicsGetImageFromCurrentImageContext()
      UIGraphicsEndImageContext()
      images.append(image)
      
      let offsetY = scrollView.contentOffset.y
      scrollView.setContentOffset(CGPoint(x: 0, y: offsetY + boundsHeight), animated: false)
      contentHeight = contentHeight - boundsHeight
    }
    scrollView.setContentOffset(oldOffset, animated: false)
    
    // concate the page
    UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, false, scale)
    for (index,image) in images.enumerate(){
      image.drawInRect(CGRect(x: CGFloat(0), y: boundsHeight * CGFloat(index), width: boundsWidth, height: boundsHeight ))
    }
    let fullImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return fullImage
  }
2017年12月25日 22:49