Search code examples
swiftmacosimagescreenshotpasteboard

Generating a png-File on the desktop from a screenshot made to the pasteboard on macOS


Hi stackoverflow forum,

I've written a macOS swiftUI app, which checks the availability to a website, i.e. to my cloud. In the case, the access is lost, I'm creating a screenshot to the pasteboard and I'm generating a POST request to a PHP script on my webserver, to send an information email to my email address.

I'm using the following 2 functions, to generate the screenshot and save it to the pasteboard. All based on the tutorial (very good!) from Grace Huang (https://www.youtube.com/watch?v=7nnYsRrtweA)

//MARK: - Ein Screenshot wird erstellt und im Pasteboard abgespeichert

func erfolgreichEinScreenshotErstellt() -> Bool{
    let task = Process()
    task.launchPath = "/usr/sbin/screencapture"
    task.arguments = ["-c"]
    task.launch()
    task.waitUntilExit()
    let status = task.terminationStatus
    return status == 0
}

//MARK: - Das gecaptured Bild wird aus dem Pastboard geholt und in das image geladen

func dasBildAbholenUndZeigen() -> NSImage {
    let pasteboard = NSPasteboard.general
    guard pasteboard.canReadItem(withDataConformingToTypes:
        NSImage.imageTypes) else{
        return NSImage()
    }
    guard let image = NSImage(pasteboard: pasteboard) else {
        return NSImage()
    }
    let tiffData = image.tiffRepresentation!
    let pngData = NSBitmapImageRep(data: tiffData)?.representation(using: .png, properties: [:])

    return image
}

Everything works fine till here!

The last step I like to do, I need a little help for: how can I save the "image" as a .png-file with a given name to my desktop for further use, i.e. generating a pdf file or so from the png file.

As you can see, I've tried the following code but than I've got stuck:

.........

    let tiffData = image.tiffRepresentation!
    let pngData = NSBitmapImageRep(data: tiffData)?.representation(using: .png, properties: [:])

Thanks in advance, if someone likes to help me with some code or a hint to a solution idea.

Best

Thomas


Solution

  • Your method is missing the actual saving to disk call:

    let tiffData = image.tiffRepresentation!
    let pngData = NSBitmapImageRep(data: tiffData)?.representation(using: .png, properties: [:])
    let path = URL(fileURLWithPath: "/Users/*YourUsername*/Desktop/testImage.png")
    try? pngData?.write(to: path) // << save to disk