-
Notifications
You must be signed in to change notification settings - Fork 59
/
ImageHelper.swift
62 lines (54 loc) · 1.96 KB
/
ImageHelper.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// ImageHelper.swift
// rem
//
// Created by Jason McGhee on 12/31/23.
//
import Foundation
import os
import SwiftUI
class ImageHelper {
private static let logger = Logger(
subsystem: Bundle.main.bundleIdentifier!,
category: String(describing: ImageHelper.self)
)
// Useful for debugging...
static func pngData(from nsImage: NSImage) -> Data? {
guard let tiffRepresentation = nsImage.tiffRepresentation,
let bitmapImage = NSBitmapImageRep(data: tiffRepresentation) else {
logger.error("Failed to get TIFF representation of NSImage")
return nil
}
guard let pngData = bitmapImage.representation(using: .png, properties: [:]) else {
logger.error("Failed to convert NSImage to PNG")
return nil
}
return pngData
}
static func saveNSImage(image: NSImage, path: String) {
let pngData = pngData(from: image)
do {
if let savedir = RemFileManager.shared.getSaveDir() {
let outputPath = savedir.appendingPathComponent("\(path).png").path
let fileURL = URL(fileURLWithPath: outputPath)
try pngData?.write(to: fileURL)
logger.info("PNG file written successfully")
} else {
logger.error("Error writing PNG file")
}
} catch {
logger.error("Error writing PNG file: \(error)")
}
}
static func saveCGImage(image: CGImage, path: String) {
saveNSImage(image: NSImage(cgImage: image, size: NSZeroSize), path: path)
}
static func cropImage(image: CGImage, frame: CGRect, scale: CGFloat) -> CGImage? {
let cropZone = CGRect(
x: frame.origin.x * scale,
y: frame.origin.y * scale,
width: frame.size.width * scale,
height: frame.size.height * scale)
return image.cropping(to: cropZone)
}
}