Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature/image-interactions] VisionKit / text recognition actions for images #1283

Merged
merged 4 commits into from
Dec 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions ownCloud.xcodeproj/xcshareddata/xcschemes/ownCloud.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,11 @@
value = "string:https://www.owncloud.com/"
isEnabled = "NO">
</EnvironmentVariable>
<EnvironmentVariable
key = "oc:action.allow-image-interactions"
value = "false"
isEnabled = "NO">
</EnvironmentVariable>
</EnvironmentVariables>
<AdditionalOptions>
<AdditionalOption
Expand Down
7 changes: 7 additions & 0 deletions ownCloud/Client/Viewer/Image/ImageDisplayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,13 @@ extension ImageDisplayViewController: DisplayExtension {
extension ImageDisplayViewController: UIGestureRecognizerDelegate {

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldBeRequiredToFailBy otherGestureRecognizer: UIGestureRecognizer) -> Bool {
if gestureRecognizer == tapToZoomGestureRecognizer,
scrollView?.hasActiveImageAnalysisSelection == true, // allow selection when VisionKit image analysis is active
let otherGestureRecognizer = otherGestureRecognizer as? UITapGestureRecognizer,
otherGestureRecognizer.numberOfTapsRequired == 2 {
return true
}

if gestureRecognizer === tapToZoomGestureRecognizer && otherGestureRecognizer === showHideBarsTapGestureRecognizer {
return true
}
Expand Down
84 changes: 79 additions & 5 deletions ownCloud/UI Elements/ImageScrollView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@
*/

import UIKit
import VisionKit
import ownCloudSDK
import ownCloudAppShared

class ImageScrollView: UIScrollView {

// MARK: - Constants
private let MAXIMUM_ZOOM_SCALE: CGFloat = 6.0

// MARK: - Instance Variables
private var imageView: UIImageView!
private var imageView: UIImageView?
private var imageAnalysisInteraction: Any?

// MARK: - Init
override init(frame: CGRect) {
Expand All @@ -49,12 +52,12 @@ class ImageScrollView: UIScrollView {

// MARK: - Manage Scale
private func centerImage() {
guard imageView != nil else {
guard let imageView else {
return
}

let boundsSize: CGSize = bounds.size
var frameToCenter: CGRect = imageView?.frame ?? .zero
var frameToCenter: CGRect = imageView.frame

// center horizontally
if frameToCenter.size.width < boundsSize.width {
Expand Down Expand Up @@ -94,7 +97,6 @@ class ImageScrollView: UIScrollView {

// MARK: - Public API
extension ImageScrollView {

func updateScaleForRotation(size: CGSize) {
contentSize = size
setMinZoomScaleForCurrentBounds(size)
Expand All @@ -106,11 +108,58 @@ extension ImageScrollView {

func display(image: UIImage, inSize: CGSize) {
imageView?.removeFromSuperview()

imageView = UIImageView(image: image)
guard let imageView else { return }

imageView.accessibilityIdentifier = "loaded-image-gallery"
imageView.contentMode = .scaleAspectFit

addSubview(imageView)
updateScaleForRotation(size: inSize)

if imageInteractionsAllowed {
analyzeImage(image: image)
}
}

var hasActiveImageAnalysisSelection: Bool {
if #available(iOS 16, *) {
if let interaction = imageAnalysisInteraction as? ImageAnalysisInteraction {
return interaction.selectableItemsHighlighted
}
}

return false
}

func analyzeImage(image: UIImage) {
if #available(iOS 16, *) {
guard ImageAnalyzer.isSupported else {
return
}

let interaction = ImageAnalysisInteraction()
imageView?.addInteraction(interaction)

imageAnalysisInteraction = interaction

Task.detached(priority: .userInitiated, operation: {
do {
let configuration = ImageAnalyzer.Configuration([.machineReadableCode, .text, .visualLookUp])
let analyzer = ImageAnalyzer()
let analysis = try await analyzer.analyze(image, configuration: configuration)
await MainActor.run {
interaction.analysis = analysis
interaction.preferredInteractionTypes = [.automatic]
}
} catch {
await MainActor.run(body: {
interaction.preferredInteractionTypes = []
})
}
})
}
}
}

Expand All @@ -125,3 +174,28 @@ extension ImageScrollView: UIScrollViewDelegate {
}

}

// MARK: - Class Settings
public extension OCClassSettingsKey {
static let allowImageInteractions = OCClassSettingsKey("allow-image-interactions")
}

extension ImageScrollView {
static func registerImageInteractionsSettings() {
Action.registerOCClassSettingsDefaults([
.allowImageInteractions : true
], metadata: [
.allowImageInteractions : [
.type : OCClassSettingsMetadataType.boolean,
.label : "Allow Image Interactions",
.description : "Allow (true) or disallow (false) text/selection/OCR interactions with images.",
.status : OCClassSettingsKeyStatus.advanced,
.category : "Actions"
]
])
}

var imageInteractionsAllowed: Bool {
return Action.classSetting(forOCClassSettingsKey: .allowImageInteractions) as? Bool ?? true
}
}