Created
August 30, 2021 20:54
-
-
Save dodderss/e0a2a02b5e32018139a47f5e53ff3bb4 to your computer and use it in GitHub Desktop.
Save NSImage to file macOS SwiftUI
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
savePNG(image: NSImage, path: String)