Skip to content

Commit

Permalink
Update macOS to use new exporting functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Arclite committed Jul 15, 2024
1 parent c20e3da commit b9b73b8
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Automator/Sources/RedactOperation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class RedactOperation: Operation, @unchecked Sendable {
let redactions = matchingObservations.map { Redaction($0, color: .black) }

guard let inputImage = input.image else { throw RedactActionExportError.noImageForInput }
let redactedImage = try await PhotoExportRenderer(image: inputImage, redactions: redactions).render()
let redactedImage = try await PhotoRenderer(image: inputImage, redactions: redactions).render()
let writeURL = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString, conformingTo: input.fileType ?? .png)

os_log("export representations: %{public}@", String(describing: redactedImage.representations))
Expand Down
4 changes: 2 additions & 2 deletions Modules/Capabilities/Rendering/Sources/PhotoRenderer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public actor PhotoRenderer {
#if canImport(AppKit) && !targetEnvironment(macCatalyst)
public init(image: NSImage, redactions: [Redaction]) throws {
guard let sourceImage = image.cgImage(forProposedRect: nil, context: nil, hints: nil)
else { throw PhotoExportRenderError.noCGImage }
else { throw PhotoRenderError.noCGImage }

self.sourceImage = sourceImage
self.redactions = redactions
Expand All @@ -41,7 +41,7 @@ public actor PhotoRenderer {
bitsPerPixel: 32
),
let graphicsContext = NSGraphicsContext(bitmapImageRep: imageRep)
else { throw PhotoExportRenderError.noCurrentGraphicsContext }
else { throw PhotoRenderError.noCurrentGraphicsContext }
let context = graphicsContext.cgContext

let cgImage = try render(
Expand Down
16 changes: 10 additions & 6 deletions Modules/Legacy/Core/Sources/Desktop/Toolbar Items/ShareItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,26 @@ class ShareItem: NSSharingServicePickerToolbarItem, UIActivityItemsConfiguration
guard let delegate = delegate, delegate.canExportImage else { return [] }

let itemProvider = NSItemProvider()
itemProvider.registerDataRepresentation(forTypeIdentifier: UTType.png.identifier, visibility: .all, loadHandler: { [weak self] loadHandler -> Progress? in
itemProvider.registerFileRepresentation(forTypeIdentifier: UTType.png.identifier, visibility: .all) { [weak self] loadHandler -> Progress? in
Task { [weak self] in
let image = await self?.delegate?.exportImage()
loadHandler(image?.pngData(), nil)
self?.delegate?.didExportImage()
do {
let url = try await self?.delegate?.exportedURL()
loadHandler(url, false, nil)
self?.delegate?.didExportImage()
} catch {
loadHandler(nil, false, error)
}
}
return nil
})
}

return [itemProvider]
}
}

protocol ShareItemDelegate: AnyObject {
var canExportImage: Bool { get }
func exportImage() async -> UIImage?
func exportedURL() async throws -> URL?
func didExportImage()
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -20,77 +20,37 @@ extension PhotoEditingViewController {

@objc func save(_ sender: Any) {
guard let exportURL = fileURLProvider?.representedFileURL else { return saveAs(sender) }
guard let imageType = imageType else { return present(.missingImageType) }

Task { [weak self] in
let image = try? await exportImage()
let data: Data?
Task { @MainActor [weak self] in
guard let self else { return }

switch imageType {
case .jpeg:
data = image?.jpegData(compressionQuality: 0.9)
default:
data = image?.pngData()
}

guard let exportData = data else {
DispatchQueue.main.async { [weak self] in
self?.present(.noImageData)
}
return
}
do {
try exportData.write(to: exportURL)
self?.clearHasMadeEdits()
try await FileManager.default.copyItem(at: preparedURL, to: exportURL)
clearHasMadeEdits()

Defaults.numberOfSaves += 1
DispatchQueue.main.async { [weak self] in
AppRatingsPrompter().displayRatingsPrompt(in: self?.view.window?.windowScene)
}
AppRatingsPrompter().displayRatingsPrompt(in: view.window?.windowScene)
} catch {
ErrorHandler().log(error)
}
}
}

@objc func saveAs(_ sender: Any) {
guard let imageType = imageType else { return present(.missingImageType) }

let representedURLName = fileURLProvider?.representedFileURL?.lastPathComponent ?? "\(ExportingStrings.PhotoEditingExporter.defaultImageName).\(imageType.preferredFilenameExtension ?? "png")"
let temporaryURL = URL(fileURLWithPath: NSTemporaryDirectory())
.appendingPathComponent(representedURLName)

Task { [weak self] in
let image = try? await exportImage()
let data: Data?

switch imageType {
case .jpeg:
data = image?.jpegData(compressionQuality: 0.9)
default:
data = image?.pngData()
}

guard let exportData = data else {
DispatchQueue.main.async { [weak self] in
self?.present(.noImageData)
}
return
}
Task { @MainActor [weak self] in
do {
try exportData.write(to: temporaryURL)
self?.clearHasMadeEdits()

Defaults.numberOfSaves += 1
DispatchQueue.main.async { [weak self] in
let saveViewController = DesktopSaveViewController(url: temporaryURL) { [weak self] urls in
AppRatingsPrompter().displayRatingsPrompt(in: self?.view.window?.windowScene)
if let exportURL = urls.first {
self?.fileURLProvider?.updateRepresentedFileURL(to: exportURL)
}
guard let self else { return }
let temporaryURL = try await preparedURL
let saveViewController = DesktopSaveViewController(url: temporaryURL) { [weak self] urls in
AppRatingsPrompter().displayRatingsPrompt(in: self?.view.window?.windowScene)
if let exportURL = urls.first {
self?.fileURLProvider?.updateRepresentedFileURL(to: exportURL)
}
self?.present(saveViewController, animated: true, completion: nil)
}
present(saveViewController, animated: true)
} catch {
ErrorHandler().log(error)
}
Expand Down
4 changes: 2 additions & 2 deletions Modules/Legacy/Core/Sources/Scene/DesktopSceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ class DesktopSceneDelegate: NSObject, UIWindowSceneDelegate, NSToolbarDelegate,

var canExportImage: Bool { return editingViewController != nil }

func exportImage() async -> UIImage? {
return try? await editingViewController?.exportImage()
func exportedURL() async throws -> URL? {
return try await editingViewController?.preparedURL
}

func didExportImage() {
Expand Down

0 comments on commit b9b73b8

Please sign in to comment.