Skip to content

Instantly share code, notes, and snippets.

@WilliamD47
Created August 30, 2021 20:54
Show Gist options
  • Save WilliamD47/e0a2a02b5e32018139a47f5e53ff3bb4 to your computer and use it in GitHub Desktop.
Save WilliamD47/e0a2a02b5e32018139a47f5e53ff3bb4 to your computer and use it in GitHub Desktop.
Save NSImage to file macOS SwiftUI
func savePNG(image: NSImage, path:String) {
let imageRep = NSBitmapImageRep(data: image.tiffRepresentation!)
let pngData = imageRep?.representation(using: .png, properties: [:])
do {
try pngData!.write(to: URL(fileURLWithPath: path))
} catch {
print(error)
}
}
@WilliamD47
Copy link
Author

Usage:
savePNG(image: NSImage, path: String)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment