加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在swift中获取所有webview的完整屏幕截图

发布时间:2020-12-14 04:35:04 所属栏目:百科 来源:网络整理
导读:有谁知道如何使用 swift中的代码(不是屏幕,因为webview很长,我们必须滚动查看所有内容)并通过UIActivityViewController共享它,我可以为我的所有Web视图拍摄完整的屏幕截图? 解决方法 加载Web视图后,请尝试以下代码: 编辑:在模拟器中运行此代码,然后检查文
有谁知道如何使用 swift中的代码(不是屏幕,因为webview很长,我们必须滚动查看所有内容)并通过UIActivityViewController共享它,我可以为我的所有Web视图拍摄完整的屏幕截图?

解决方法

加载Web视图后,请尝试以下代码:

编辑:在模拟器中运行此代码,然后检查文档应用程序目录中的screenshot.jpeg文件

class ViewController: UIViewController,UIWebViewDelegate {

let fileDirectory = NSSearchPathForDirectoriesInDomains(
    .DocumentDirectory,.UserDomainMask,true)

@IBOutlet weak var webView: UIWebView!

override func viewDidLoad() {


    var urlPath = "http://www.bbc.com/"
    let requestUrl = NSURL(string: urlPath)
    let request = NSURLRequest(URL: requestUrl!)
    webView.loadRequest(request)
    webView.delegate = self

}

func webViewDidFinishLoad(webView: UIWebView) {


    if (!webView.loading){

        var webViewFrame = webView.frame

        webView.frame = CGRectMake(webViewFrame.origin.x,webViewFrame.origin.y,webView.scrollView.contentSize.width,webView.scrollView.contentSize.height)

        UIGraphicsBeginImageContextWithOptions(webView.scrollView.contentSize,false,0);
        self.webView.layer.renderInContext(UIGraphicsGetCurrentContext())
        var image:UIImage = UIGraphicsGetImageFromCurrentImageContext();
        var imageData = UIImageJPEGRepresentation(image,0.7)
        var currentFileName = "screenshot.jpeg"
        var imageFilePath = fileDirectory[0].stringByAppendingPathComponent(currentFileName)
        var imageFileURL = NSURL(fileURLWithPath: imageFilePath)
        imageData.writeToURL(imageFileURL!,atomically: false)
        webView.frame = webViewFrame
    }
}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读