From 4fb9990b3a37126bb29fa151e4017851ef2dbf2c Mon Sep 17 00:00:00 2001 From: Geoff Pado Date: Sun, 19 May 2024 10:35:41 -0700 Subject: [PATCH] Fix build --- .../Detections/Sources/TextDetector.swift | 39 +++++++++---------- .../Sources/TextRecognitionOperation.swift | 24 ++++++------ .../Sources/UIImageExtensions.swift | 2 +- .../Targets/Detections.swift | 1 - .../Targets/Legacy/Core.swift | 2 +- 5 files changed, 33 insertions(+), 35 deletions(-) diff --git a/Modules/Capabilities/Detections/Sources/TextDetector.swift b/Modules/Capabilities/Detections/Sources/TextDetector.swift index 6e8adbd6..531f2e8c 100644 --- a/Modules/Capabilities/Detections/Sources/TextDetector.swift +++ b/Modules/Capabilities/Detections/Sources/TextDetector.swift @@ -12,8 +12,8 @@ import UIKit import Vision open class TextDetector: NSObject { - #if canImport(UIKit) && targetEnvironment(macCatalyst) - public func detectTextRectangles(in image: UIImage, completionHandler: (([TextRectangleObservation]?) -> Void)? = nil) { + #if canImport(AppKit) && !targetEnvironment(macCatalyst) + public func detectTextRectangles(in image: NSImage, completionHandler: (([TextRectangleObservation]?) -> Void)? = nil) { guard let detectionOperation = TextRectangleDetectionOperation(image: image) else { completionHandler?(nil) return @@ -26,8 +26,8 @@ open class TextDetector: NSObject { operationQueue.addOperation(detectionOperation) } - #elseif canImport(AppKit) - public func detectTextRectangles(in image: NSImage, completionHandler: (([TextRectangleObservation]?) -> Void)? = nil) { + #elseif canImport(UIKit) + public func detectTextRectangles(in image: UIImage, completionHandler: (([TextRectangleObservation]?) -> Void)? = nil) { guard let detectionOperation = TextRectangleDetectionOperation(image: image) else { completionHandler?(nil) return @@ -76,38 +76,37 @@ open class TextDetector: NSObject { .flatMap(\.allWordObservations) } - #if canImport(UIKit) && targetEnvironment(macCatalyst) - public func detectWords(in image: UIImage, completionHandler: @escaping (([WordObservation]?) -> Void)) { + #if canImport(AppKit) && !targetEnvironment(macCatalyst) + @available(macOS 10.15, *) + public func detectWords(in image: NSImage, completionHandler: @escaping (([WordObservation]?) -> Void)) { guard let recognitionOperation = try? TextRecognitionOperation(image: image) else { return completionHandler(nil) } Task { await completionHandler(detectWords(with: recognitionOperation)) } } - open func detectText(in image: UIImage) async throws -> [RecognizedTextObservation] { - let recognitionOperation = try TextRecognitionOperation(image: image) - return await detectText(with: recognitionOperation) - } - - public func detectText(in image: UIImage, completionHandler: @escaping (([RecognizedTextObservation]?) -> Void)) { + public func detectText(in image: NSImage, completionHandler: @escaping (([RecognizedTextObservation]?) -> Void)) { + guard let recognitionOperation = try? TextRecognitionOperation(image: image) else { return completionHandler(nil) } Task { - await completionHandler(try? detectText(in: image)) + await completionHandler(detectText(with: recognitionOperation)) } } - - #elseif canImport(AppKit) - @available(macOS 10.15, *) - public func detectWords(in image: NSImage, completionHandler: @escaping (([WordObservation]?) -> Void)) { + #elseif canImport(UIKit) + public func detectWords(in image: UIImage, completionHandler: @escaping (([WordObservation]?) -> Void)) { guard let recognitionOperation = try? TextRecognitionOperation(image: image) else { return completionHandler(nil) } Task { await completionHandler(detectWords(with: recognitionOperation)) } } - public func detectText(in image: NSImage, completionHandler: @escaping (([RecognizedTextObservation]?) -> Void)) { - guard let recognitionOperation = try? TextRecognitionOperation(image: image) else { return completionHandler(nil) } + open func detectText(in image: UIImage) async throws -> [RecognizedTextObservation] { + let recognitionOperation = try TextRecognitionOperation(image: image) + return await detectText(with: recognitionOperation) + } + + public func detectText(in image: UIImage, completionHandler: @escaping (([RecognizedTextObservation]?) -> Void)) { Task { - await completionHandler(detectText(with: recognitionOperation)) + await completionHandler(try? detectText(in: image)) } } #endif diff --git a/Modules/Capabilities/Detections/Sources/TextRecognitionOperation.swift b/Modules/Capabilities/Detections/Sources/TextRecognitionOperation.swift index 8019341f..625cc2f3 100644 --- a/Modules/Capabilities/Detections/Sources/TextRecognitionOperation.swift +++ b/Modules/Capabilities/Detections/Sources/TextRecognitionOperation.swift @@ -5,22 +5,14 @@ import Foundation import OSLog import Vision -#if canImport(UIKit) && targetEnvironment(macCatalyst) -import UIKit -#elseif canImport(AppKit) +#if canImport(AppKit) && !targetEnvironment(macCatalyst) import AppKit +#elseif canImport(UIKit) +import UIKit #endif class TextRecognitionOperation: Operation { - #if canImport(UIKit) && targetEnvironment(macCatalyst) - init(image: UIImage) throws { - guard let cgImage = image.cgImage else { throw TextRecognitionOperationError.cannotCreateCGImageFromImage } - self.imageRequestHandler = VNImageRequestHandler(cgImage: cgImage, orientation: image.imageOrientation.cgImagePropertyOrientation) - self.imageSize = CGSize(width: cgImage.width, height: cgImage.height) - - super.init() - } - #elseif canImport(AppKit) + #if canImport(AppKit) && !targetEnvironment(macCatalyst) init(image: NSImage) throws { var imageRect = NSRect(origin: .zero, size: image.size) guard let cgImage = image.cgImage(forProposedRect: &imageRect, context: nil, hints: nil) else { throw TextRecognitionOperationError.cannotCreateCGImageFromImage } @@ -28,6 +20,14 @@ class TextRecognitionOperation: Operation { self.imageRequestHandler = VNImageRequestHandler(cgImage: cgImage, orientation: .up) self.imageSize = CGSize(width: cgImage.width, height: cgImage.height) } + #elseif canImport(UIKit) + init(image: UIImage) throws { + guard let cgImage = image.cgImage else { throw TextRecognitionOperationError.cannotCreateCGImageFromImage } + self.imageRequestHandler = VNImageRequestHandler(cgImage: cgImage, orientation: image.imageOrientation.cgImagePropertyOrientation) + self.imageSize = CGSize(width: cgImage.width, height: cgImage.height) + + super.init() + } #endif var recognizedTextResults: [VNRecognizedTextObservation]? diff --git a/Modules/Capabilities/Detections/Sources/UIImageExtensions.swift b/Modules/Capabilities/Detections/Sources/UIImageExtensions.swift index 755114bd..d8d21b4f 100644 --- a/Modules/Capabilities/Detections/Sources/UIImageExtensions.swift +++ b/Modules/Capabilities/Detections/Sources/UIImageExtensions.swift @@ -1,7 +1,7 @@ // Created by Geoff Pado on 5/13/24. // Copyright © 2024 Cocoatype, LLC. All rights reserved. -#if canImport(UIKit) && targetEnvironment(macCatalyst) +#if canImport(UIKit) import UIKit extension UIImage.Orientation { diff --git a/Tuist/ProjectDescriptionHelpers/Targets/Detections.swift b/Tuist/ProjectDescriptionHelpers/Targets/Detections.swift index 1b9a95fc..8321933f 100644 --- a/Tuist/ProjectDescriptionHelpers/Targets/Detections.swift +++ b/Tuist/ProjectDescriptionHelpers/Targets/Detections.swift @@ -17,4 +17,3 @@ public enum Detections { ] ) } - diff --git a/Tuist/ProjectDescriptionHelpers/Targets/Legacy/Core.swift b/Tuist/ProjectDescriptionHelpers/Targets/Legacy/Core.swift index a0271907..1c3d32ff 100644 --- a/Tuist/ProjectDescriptionHelpers/Targets/Legacy/Core.swift +++ b/Tuist/ProjectDescriptionHelpers/Targets/Legacy/Core.swift @@ -12,7 +12,7 @@ public enum Core { .target(AppRatings.target), .target(Defaults.target), .target(DesignSystem.target), - .target(Detections.target(sdk:.catalyst)), + .target(Detections.target(sdk: .catalyst)), .target(Editing.target), .target(PurchaseMarketing.target), .target(Purchasing.target),