Skip to content

Instantly share code, notes, and snippets.

@westerlund
Created November 29, 2015 10:25
Show Gist options
  • Save westerlund/e1b99e21615bb18bc380 to your computer and use it in GitHub Desktop.
Save westerlund/e1b99e21615bb18bc380 to your computer and use it in GitHub Desktop.
Write NSImage to disk
extension NSImage {
func writeToFile(file: String, atomically: Bool, usingType type: NSBitmapImageFileType) -> Bool {
let properties = [NSImageCompressionFactor: 1.0]
guard
let imageData = TIFFRepresentation,
imageRep = NSBitmapImageRep(data: imageData),
fileData = imageRep.representationUsingType(type, properties: properties) else {
return false
}
return fileData.writeToFile(file, atomically: atomically)
}
}
@westerlund
Copy link
Author

Usage:

let filePath = ...
someImage.writeToFile(filePath, atomically: true, usingType: .NSPNGFileType) // as png
someImage.writeToFile(filePath, atomically: true, usingType: .NSJPEGFileType) // as jpg

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