Hello Friends Today, through this tutorial, I will tell you How to Take Screenshot of a Website From Url Using Swift? In Swift, you can use the `WebKit` framework to load a website and capture its content as an image. Below is a basic example using `WKWebView` to load a website and capture a screenshot.
Create a Swift script (e.g., `screenshot.swift`) with the following content:
import Foundation
import WebKit
class ScreenshotDelegate: NSObject, WKNavigationDelegate {
var webView: WKWebView?
var outputFileName: String
init(outputFileName: String) {
self.outputFileName = outputFileName
super.init()
// Initialize WKWebView
let webConfiguration = WKWebViewConfiguration()
self.webView = WKWebView(frame: .zero, configuration: webConfiguration)
self.webView?.navigationDelegate = self
}
func takeScreenshot(urlString: String) {
guard let url = URL(string: urlString) else {
print("Invalid URL")
return
}
// Load the webpage
let request = URLRequest(url: url)
webView?.load(request)
// Run the main loop until the navigation is complete
RunLoop.main.run()
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
// Capture screenshot after the webpage has loaded
captureScreenshot()
}
func captureScreenshot() {
guard let webView = webView else {
print("WebView not initialized")
return
}
// Get the content size of the webpage
let contentSize = webView.scrollView.contentSize
// Create an image context with the size of the content
UIGraphicsBeginImageContextWithOptions(contentSize, false, 0)
// Render the content into the image context
webView.scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)
// Get the screenshot as an image
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
// End the image context
UIGraphicsEndImageContext()
// Convert the image to PNG data
if let imageData = screenshot?.pngData() {
// Save the PNG data to a file
let outputPath = FileManager.default.currentDirectoryPath + "/" + outputFileName
do {
try imageData.write(to: URL(fileURLWithPath: outputPath))
print("Screenshot saved to: \(outputPath)")
} catch {
print("Error saving screenshot: \(error.localizedDescription)")
}
}
// Stop the main loop
CFRunLoopStop(CFRunLoopGetCurrent())
}
}
// Replace 'https://example.com' with the URL of the website you want to capture
let urlToCapture = "https://example.com"
// Replace 'screenshot.png' with the desired output file name
let outputFileName = "screenshot.png"
// Create the ScreenshotDelegate instance
let screenshotDelegate = ScreenshotDelegate(outputFileName: outputFileName)
// Take the screenshot
screenshotDelegate.takeScreenshot(urlString: urlToCapture)
Make sure to replace:
- `"https://example.com"` with the actual URL of the website you want to capture.
- `"screenshot.png"` with the desired output file name.
Save the script and run it using the Swift interpreter:
swift screenshot.swiftThis script uses `WKWebView` to load a webpage and capture its content as an image. It saves the screenshot as a PNG file in the current working directory. Note that the script runs in a synchronous manner, and the main loop is used to wait for the webpage to finish loading before capturing the screenshot.