Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
  • Loading branch information
Arclite committed May 19, 2024
1 parent a3fabf2 commit 4fb9990
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 35 deletions.
39 changes: 19 additions & 20 deletions Modules/Capabilities/Detections/Sources/TextDetector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@ 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 }

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]?
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
1 change: 0 additions & 1 deletion Tuist/ProjectDescriptionHelpers/Targets/Detections.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@ public enum Detections {
]
)
}

2 changes: 1 addition & 1 deletion Tuist/ProjectDescriptionHelpers/Targets/Legacy/Core.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit 4fb9990

Please sign in to comment.