in Diffusion-macOS/DiffusionImage+macOS.swift [14:37]
func save(cgImage: CGImage, filename: String?) -> URL? {
let nsImage = NSImage(cgImage: cgImage, size: NSSize(width: cgImage.width, height: cgImage.height))
let appSupportURL = Settings.shared.tempStorageURL()
let fn = filename ?? "diffusion_generated_image"
let fileURL = appSupportURL
.appendingPathComponent(fn)
.appendingPathExtension("png")
// Save the image as a temporary file
if let tiffData = nsImage.tiffRepresentation,
let bitmap = NSBitmapImageRep(data: tiffData),
let pngData = bitmap.representation(using: .png, properties: [:]) {
do {
try pngData.write(to: fileURL)
return fileURL
} catch {
print("Error saving image to temporary file: \(error)")
}
}
return nil
}