Take a full screenshot for all webview in swift
Try the following code after web view is loaded:
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
}
}
}