快速为所有 webview 截取完整屏幕截图 [英] Take a full screenshot for all webview in swift

查看:33
本文介绍了快速为所有 webview 截取完整屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Does anyone know how can I take a full screenshot for all my web view using code in swift ( not the screen ! because the webview is long and we must scroll to view the all content ) and share it via UIActivityViewController?

推荐答案

在加载 web view 后尝试以下代码:

Try the following code after web view is loaded:

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

EDITED: run this code in simulator then check the screenshot.jpeg file in your documents app directory

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
    }
}

}

这篇关于快速为所有 webview 截取完整屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆