【问题标题】:Take a full screenshot for all webview in swift快速截取所有 webview 的完整截图
【发布时间】:2015-01-16 05:21:43
【问题描述】:

有谁知道我如何使用 swift 中的代码(而不是屏幕!因为 webview 很长,我们必须滚动才能查看所有内容)为我的所有 web 视图截取完整的屏幕截图并通过UIActivityViewController 共享它?

【问题讨论】:

    标签: swift uiactivityviewcontroller


    【解决方案1】:

    加载网页视图后尝试以下代码:

    已编辑:在模拟器中运行此代码,然后检查您的文档应用程序目录中的 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
        }
    }
    
    }
    

    【讨论】:

    • 我收到一个错误:fileDirectory[0] anw,我尝试这样的代码: 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(); webView.frame = webViewFrame 但屏幕截图并不适用于所有 webview:/ 就像屏幕截图一样
    • 关于错误使用以下var fileDirectory = NSSearchPathForDirectoriesInDomains( .DocumentDirectory, .UserDomainMask, true)。关于屏幕截图,您确定 webview 已完全加载吗?刚刚在不同的网站上试了一下,效果很好
    • 关于第一个的更正:我们现在需要编辑这个:var imageFileURL = NSURL(fileURLWithPath: imageFilePath) 关于第二个:是的,我是 usre,因为 webview 是本地的(在我的项目中我有html 文件)在我的 UIActivityViewController 中,我调用:图像( UIImage ),而不是另一个 var
    • 当我设置在:webView.scrollView.contentSize.height 这个值:10000 的地方,所有的内容都是可见的,但是创建的图像太长了......我不知道是什么问题与 contentSize.height ?! @zellb
    • 我想我知道问题出在哪里!图像创建内容顶部的空白=底部缺少的空间! .那么我该如何解决这个问题呢? @zellb
    【解决方案2】:

    在 Swift 5 中使用 WKWebView 的解决方案

    extension ViewController: WKNavigationDelegate {
        func webView(_ webView: WKWebView,didFinish navigation: WKNavigation!) {
    
            let configuration = WKSnapshotConfiguration()
            configuration.rect = CGRect(origin: .zero, size: (self.webView.scrollView.contentSize))
    
            webView.takeSnapshot(with: configuration) {image, error in
                if let image = image {
                    self.saveImage(imageName: "snapshot.jpg", image: image)
                }
            }
        }
    }
    

    【讨论】:

    • 太棒了!你的代码工作!!!但有一个唯一的问题:如果你向下滚动 webview,快照是不正确的
    • 我在 iOS 13+ 上得到空白图像。在较低的设备上工作正常
    • 使用 XCode 11.5 进行检查,此答案不再捕获整个页面高度。
    • 添加了接受答案的技巧(将 webview 调整为 webView.scrollView.contentSize.height 的高度)有效。
    • 这适用于我在 iOS 13.3 中,但不适用于 13.4 和 12.4.3
    猜你喜欢
    • 2018-11-13
    • 2015-04-06
    • 1970-01-01
    • 2011-02-24
    • 1970-01-01
    • 2016-08-31
    • 2014-01-22
    • 1970-01-01
    • 2010-11-19
    相关资源
    最近更新 更多