Add more examples to the macOS sample

This commit is contained in:
Simon 2018-12-26 22:05:47 +08:00
parent 07e5766afc
commit 7dc0ad949b
2 changed files with 41 additions and 22 deletions

View File

@ -0,0 +1,27 @@
//
// NSImage+PNG.swift
// ImagesExample-macOS
//
// Created by Simon Lee on 12/26/18.
//
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)")
}
}
}

View File

@ -7,28 +7,20 @@
import PlaceholderKit
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))
let images = [
PlaceholderBuilder().coloredBackground(color: .red,
size: CGSize(width: 100, height: 100)),
PlaceholderBuilder().coloredBackground(color: .blue,
size: CGSize(width: 100, height: 200)),
PlaceholderBuilder().coloredBackground(color: .green,
size: CGSize(width: 200, height: 100)),
PlaceholderBuilder().coloredBackground(color: .yellow,
size: CGSize(width: 320, height: 480))
].compactMap({$0})
let desktopURL = FileManager.default.urls(for: .downloadsDirectory, in: .userDomainMask).first!
let fileURL = desktopURL.appendingPathComponent("placeholder.png")
image?.writePNG(toURL: fileURL)
images.enumerated().forEach { index, image in
let fileURL = desktopURL.appendingPathComponent("placeholder-\(index).png")
image.writePNG(toURL: fileURL)
}