42 lines
2.0 KiB
Swift
42 lines
2.0 KiB
Swift
import Foundation
|
|
import CoreGraphics
|
|
import ImageIO
|
|
import UniformTypeIdentifiers
|
|
import PDFKit
|
|
|
|
func writeSolidPNG(pixelWidth: Int, pixelHeight: Int,
|
|
red: UInt8, green: UInt8, blue: UInt8,
|
|
to url: URL) throws {
|
|
let colorSpace = CGColorSpaceCreateDeviceRGB()
|
|
guard let context = CGContext(data: nil, width: pixelWidth, height: pixelHeight,
|
|
bitsPerComponent: 8, bytesPerRow: pixelWidth * 4, space: colorSpace,
|
|
bitmapInfo: CGImageAlphaInfo.premultipliedLast.rawValue)
|
|
else { throw NSError(domain: "Test", code: 1, userInfo: nil) }
|
|
context.setFillColor(CGColor(red: CGFloat(red) / 255,
|
|
green: CGFloat(green) / 255,
|
|
blue: CGFloat(blue) / 255, alpha: 1))
|
|
context.fill(CGRect(x: 0, y: 0, width: pixelWidth, height: pixelHeight))
|
|
guard let image = context.makeImage(),
|
|
let destination = CGImageDestinationCreateWithURL(
|
|
url as CFURL, UTType.png.identifier as CFString, 1, nil)
|
|
else { throw NSError(domain: "Test", code: 2, userInfo: nil) }
|
|
CGImageDestinationAddImage(destination, image, nil)
|
|
guard CGImageDestinationFinalize(destination) else {
|
|
throw NSError(domain: "Test", code: 3, userInfo: nil)
|
|
}
|
|
}
|
|
|
|
let workDir = URL(fileURLWithPath: FileManager.default.currentDirectoryPath).appendingPathComponent("my_verify_output")
|
|
try? FileManager.default.removeItem(at: workDir)
|
|
try! FileManager.default.createDirectory(at: workDir, withIntermediateDirectories: true)
|
|
|
|
let url1 = workDir.appendingPathComponent("img1.png")
|
|
let url2 = workDir.appendingPathComponent("img2.png")
|
|
let url3 = workDir.appendingPathComponent("img3.png")
|
|
|
|
try! writeSolidPNG(pixelWidth: 1600, pixelHeight: 900, red: 220, green: 20, blue: 20, to: url1)
|
|
try! writeSolidPNG(pixelWidth: 900, pixelHeight: 1600, red: 20, green: 160, blue: 40, to: url2)
|
|
try! writeSolidPNG(pixelWidth: 2880, pixelHeight: 1620, red: 30, green: 70, blue: 220, to: url3)
|
|
|
|
print("Generated PNGs successfully.")
|