In this example we will capture the screen shot with a button in the Navigation Bar. We save the image in both JPEG and PNG formats in the application directory.
import UIKit
class ViewController1: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "Main View"
//setup the screen
let imageView = UIImageView()
imageView.frame = CGRect(x: 50, y: 50, width: 200, height: 200)
imageView.contentMode = .scaleAspectFit
view.addSubview(imageView)
let myImageName = "apple.png"
let myImage = UIImage(named: myImageName)
imageView.image = myImage
//add a bar button to capture our screen
let capture = UIBarButtonItem(title: "Capture Screen", style: .done, target: self, action: #selector(captureScreen))
self.navigationItem.rightBarButtonItems = [capture]
}
@objc func captureScreen(sender: UIButton!) {
var screenshotImage :UIImage?
let scale = UIScreen.main.scale
let layer = UIApplication.shared.keyWindow!.layer
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
//If you don't want the Navigation Bar
//let layer = self.view.layer
//UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
let context = UIGraphicsGetCurrentContext()!
layer.render(in:context)
screenshotImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//get the documents directory url
let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
//choose your JPEG image file name
let fileName = "myImage.jpg"
//create the destination file url to save your image
let fileURL = documentsDirectory.appendingPathComponent(fileName)
//get your UIImage jpeg data representation and check if the destination file url already exists
if let data = screenshotImage?.jpegData(compressionQuality: 1.0),
!FileManager.default.fileExists(atPath: fileURL.path) {
do {
// writes the image data to disk
try data.write(to: fileURL)
print("JPEG image file saved")
} catch {
print("Error saving image file:", error)
}
}
//choose your PNG image file name
let fileName2 = "myImage.png"
//create the destination file url to save your image
let fileURL2 = documentsDirectory.appendingPathComponent(fileName2)
//get your UIImage png data representation and check if the destination file url already exists
if let data = screenshotImage!.pngData(),
!FileManager.default.fileExists(atPath: fileURL2.path) {
do {
// writes the image data to disk
try data.write(to: fileURL2)
print("PNG image file saved")
} catch {
print("Error saving image file:", error)
}
}
//let's check if they are really saved and what's the path to the image files
let fileManager = FileManager.default
let documentsURL = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first!
do {
let directoryContents = try fileManager.contentsOfDirectory(at: documentsURL, includingPropertiesForKeys: nil)
print(directoryContents)
} catch {
print("Error while enumerating files \(documentsURL.path): \(error.localizedDescription)")
}
}
}
No comments:
Post a Comment
NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.