对 swift 中的所有 webview 进行完整截图
Take a full screenshot for all webview in swift
有谁知道如何使用 swift 中的代码为我的所有网页视图截取完整屏幕截图(不是屏幕!因为网页视图很长,我们必须滚动才能查看所有内容)并分享通过 UIActivityViewController
?
加载网络视图后尝试以下代码:
已编辑:运行模拟器中的这段代码然后检查文档应用程序目录中的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
}
}
}
WKWebView
在 Swift 5
中的解决方案
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)
}
}
}
}
有谁知道如何使用 swift 中的代码为我的所有网页视图截取完整屏幕截图(不是屏幕!因为网页视图很长,我们必须滚动才能查看所有内容)并分享通过 UIActivityViewController
?
加载网络视图后尝试以下代码:
已编辑:运行模拟器中的这段代码然后检查文档应用程序目录中的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
}
}
}
WKWebView
在 Swift 5
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)
}
}
}
}