Fix mirrored image export with CGBitmapContext renderer

Draw base CGImage without coordinate flip, apply flip+scale only for
annotations. Save via direct NSBitmapImageRep access instead of
tiffRepresentation to avoid transform issues. Also redirect clipboard
images to saveFolder instead of temp directory.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-08-30 18:52:49 +02:00
co-authored by Claude Opus 4.6
parent 8d6c2213ad
commit 188b952be7
2 changed files with 51 additions and 16 deletions
+23 -6
View File
@@ -242,24 +242,41 @@ final class AppCoordinator: ObservableObject {
private func openEditor(image: NSImage, sourceURL: URL) {
let controller = EditorWindowController(image: image, sourceURL: sourceURL) { [weak self] annotatedImage in
guard let self, let annotatedImage else { return }
self.saveAnnotatedImage(annotatedImage, replacingFileAt: sourceURL)
let targetURL: URL
if sourceURL.path.hasPrefix(NSTemporaryDirectory()) {
try? FileManager.default.createDirectory(at: saveFolder, withIntermediateDirectories: true)
targetURL = saveFolder.appendingPathComponent(Self.filename(ext: imageFormat.fileExtension))
} else {
targetURL = sourceURL
}
self.saveAnnotatedImage(annotatedImage, to: targetURL)
}
editorController = controller
controller.showEditor()
}
private func saveAnnotatedImage(_ image: NSImage, replacingFileAt url: URL) {
guard let tiffData = image.tiffRepresentation,
let bitmap = NSBitmapImageRep(data: tiffData) else { return }
private func saveAnnotatedImage(_ image: NSImage, to url: URL) {
let bitmap: NSBitmapImageRep
if let rep = image.representations.first as? NSBitmapImageRep {
bitmap = rep
} else if let cgRef = image.cgImage(forProposedRect: nil, context: nil, hints: nil) {
bitmap = NSBitmapImageRep(cgImage: cgRef)
} else {
NSLog("SnipMC: could not create bitmap for saving")
return
}
let fileType: NSBitmapImageRep.FileType = imageFormat == .jpg ? .jpeg : .png
let properties: [NSBitmapImageRep.PropertyKey: Any] = fileType == .jpeg
? [.compressionFactor: 0.9] : [:]
guard let data = bitmap.representation(using: fileType, properties: properties) else { return }
guard let data = bitmap.representation(using: fileType, properties: properties) else {
NSLog("SnipMC: could not encode image as \(imageFormat.rawValue)")
return
}
do {
try data.write(to: url, options: .atomic)
NSLog("SnipMC: saved to \(url.path)")
} catch {
NSLog("SnipMC: failed to save annotated image: \(error)")
}