Save a screenshot as pdf or high resolution-image using Swift 5 xcode 13

Viewed 577

Found here: How do I take a full screen Screenshot in Swift?

func getScreenshot() -> UIImage {
    var window: UIWindow? = UIApplication.shared.keyWindow
    window = UIApplication.shared.windows[0] as? UIWindow
    UIGraphicsBeginImageContextWithOptions(window!.frame.size, window!.isOpaque, 0.0)
    window!.layer.render(in: UIGraphicsGetCurrentContext()!)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image!
}

My image includes much stuff of text on a page in DIN A4-format. The quality of the image is very poor for printing it. Is there any chance to increase the resolution?

The best way would be to save the screenshot as pdf. Is there any help here to save and refind the image.pdf in my device.

Thx for any hint.

1 Answers

Just convert your image to pdf via PDFKit

import PDFKit
let pdfPage = PDFPage(image: screenshotImage) 
let fm = FileManager.default urls = fm.urls(for: .documentDirectory, in: .userDomainMask).first! 
let savedPDF = urls!.appendingPathComponent("pdfPage.pdf") pdfPage?.document?.write(toFile: "pdfPage")
Related