最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

iOS 對當(dāng)前webView進行截屏的方法

 更新時間:2017年04月30日 11:47:39   投稿:jingxian  
下面小編就為大家?guī)硪黄猧OS 對當(dāng)前webView進行截屏的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

UIWebView和WKWebView的截屏有所區(qū)別:

UIWebView:

func getImage(context: ServiceExecuteContext) -> UIImage { 
    //創(chuàng)建一個基于位圖的圖形上下文并指定大小 
    UIGraphicsBeginImageContextWithOptions(context.fromViewController.webView.bounds.size, true, 0) 
    //renderInContext呈現(xiàn)接受者及其子范圍到指定的上下文 
    context.fromViewController.webView.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
    //返回一個基于當(dāng)前圖形上下文的圖片 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    //移除棧頂?shù)幕诋?dāng)前位圖的圖形上下文 
    UIGraphicsEndImageContext() 
     
    //let imagRef = CGImageCreateWithImageInRect((image?.CGImage)!, context.fromViewController.webView.bounds) 
    //let newImage = UIImage.init(CGImage: imagRef!) 
    //UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);//保存圖片到照片庫 
    return image! 
  } 

UIGraphicsBeginImageContext()方法傳入唯一參數(shù),是一個CGSize變量,用來指定圖形context的大小,所以獲取屏幕截圖的時候這個size該是屏幕的大小。其實了解了這個過程,就知道這個方法可以獲取任意區(qū)域的截圖,當(dāng)然是必須當(dāng)前頁面的一部分。你需要截取哪個view的圖像,就讓這個view的layer調(diào)用renderInContext把圖形渲染進當(dāng)前圖形context。

WKWebView:

當(dāng)我嘗試去截取WKWebView的圖。截圖的結(jié)果返回給我的就僅僅只是一張背景圖, 顯然截圖失敗。通過搜索StackOverflow和Google, 我發(fā)現(xiàn)WKWebView并不能簡單的使用layer.renderInContext的方法去繪制圖形。如果直接調(diào)用layer.renderInContext需要獲取對應(yīng)的Context, 但是在WKWebView中執(zhí)行UIGraphicsGetCurrentContext()的返回結(jié)果是nil

StackOverflow提供了一種解決思路是使用UIView的drawViewHierarchyInRect方法去截取屏幕視圖。通過直接調(diào)用WKWebView的drawViewHierarchyInRect方法(afterScreenUpdates參數(shù)必須為true), 可以成功的截取WKWebView的屏幕內(nèi)容

func getImage(context: ServiceExecuteContext) -> UIImage { 
     
    UIGraphicsBeginImageContextWithOptions(context.fromViewController.webView.bounds.size, true, 0) 
    for subView: UIView in context.fromViewController.webView.subviews { 
      subView.drawViewHierarchyInRect(subView.bounds, afterScreenUpdates: true) 
    } 
    //UIApplication.sharedApplication().keyWindow?.layer.renderInContext(UIGraphicsGetCurrentContext()!) 
    let image = UIGraphicsGetImageFromCurrentImageContext() 
    UIGraphicsEndImageContext() 
     
    //let imagRef = CGImageCreateWithImageInRect((image?.CGImage)!, context.fromViewController.webView.bounds) 
    //let newImage = UIImage.init(CGImage: imagRef!) 
     
    return image! 
  } 

以上這篇iOS 對當(dāng)前webView進行截屏的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

科技| 云阳县| 莒南县| 永新县| 岳池县| 嵩明县| 千阳县| 武宁县| 炎陵县| 若尔盖县| 香格里拉县| 黄陵县| 大城县| 镇安县| 浮梁县| 常宁市| 崇文区| 肥乡县| 木里| 邵阳市| 临沭县| 华池县| 崇州市| 沿河| 济阳县| 汶川县| 深泽县| 长阳| 嘉黎县| 盐山县| 三穗县| 孟津县| 铜川市| 南部县| 中宁县| 莲花县| 寿宁县| 阿图什市| 沙洋县| 西贡区| 合水县|