Hi all,
I've written a macOS swiftUI app, which checks the availability of 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)
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
}
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