Save generated images to downloads directory

This commit is contained in:
Simon 2018-12-26 17:40:50 +08:00
parent c7b0b42e2e
commit 07e5766afc
1 changed files with 24 additions and 2 deletions

View File

@ -5,8 +5,30 @@
// Created by Simon Lee on 12/26/18.
//
import PlaceholderKit
import AppKit
import Cocoa
public extension NSImage {
public func writePNG(toURL url: URL) {
guard let data = tiffRepresentation,
let rep = NSBitmapImageRep(data: data),
let imgData = rep.representation(using: .png, properties: [.compressionFactor : NSNumber(floatLiteral: 1.0)]) else {
print("\(self.self) Error Function '\(#function)' Line: \(#line) No tiff rep found for image writing to \(url)")
return
}
do {
try imgData.write(to: url)
} catch let error {
print("\(self.self) Error Function '\(#function)' Line: \(#line) \(error.localizedDescription)")
}
}
}
let image = PlaceholderBuilder().coloredBackground(color: .red, size: CGSize(width: 100, height: 100))
print(image)
let desktopURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
let fileURL = desktopURL.appendingPathComponent("placeholder.png")
image?.writePNG(toURL: fileURL)