From facf039f972eeeb327f6d21eb94af3bbfa7f05b4 Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Wed, 8 Jun 2022 17:12:53 -0400 Subject: [PATCH] Live text in gallery view --- .../Large Image/LargeImageContentView.swift | 32 +++++++++++++++++-- .../LoadingLargeImageViewController.swift | 6 ++-- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Tusker/Screens/Large Image/LargeImageContentView.swift b/Tusker/Screens/Large Image/LargeImageContentView.swift index 5f7e335409..d9ca289768 100644 --- a/Tusker/Screens/Large Image/LargeImageContentView.swift +++ b/Tusker/Screens/Large Image/LargeImageContentView.swift @@ -9,6 +9,7 @@ import UIKit import Pachyderm import AVFoundation +import VisionKit protocol LargeImageContentView: UIView { var animationImage: UIImage? { get } @@ -16,7 +17,10 @@ protocol LargeImageContentView: UIView { func grayscaleStateChanged() } -class LargeImageImageContentView: GIFImageView, LargeImageContentView { +class LargeImageImageContentView: UIImageView, LargeImageContentView, ImageAnalysisInteractionDelegate { + + @available(iOS 16.0, *) + private static let analyzer = ImageAnalyzer() var animationImage: UIImage? { image! } @@ -25,13 +29,31 @@ class LargeImageImageContentView: GIFImageView, LargeImageContentView { } private var sourceData: Data? + private weak var owner: UIViewController? - - init(image: UIImage) { + init(image: UIImage, owner: UIViewController?) { + self.owner = owner + super.init(image: image) contentMode = .scaleAspectFit + isUserInteractionEnabled = true + if #available(iOS 16.0, *), + ImageAnalyzer.isSupported { + let interaction = ImageAnalysisInteraction() + interaction.delegate = self + interaction.preferredInteractionTypes = .automatic + addInteraction(interaction) + Task { + do { + let result = try await LargeImageImageContentView.analyzer.analyze(image, configuration: ImageAnalyzer.Configuration([.text, .machineReadableCode])) + interaction.analysis = result + } catch { + // if analysis fails, we just don't show anything + } + } + } } required init?(coder: NSCoder) { @@ -54,6 +76,10 @@ class LargeImageImageContentView: GIFImageView, LargeImageContentView { self.image = image } } + + func presentingViewController(for interaction: ImageAnalysisInteraction) -> UIViewController? { + return owner + } } class LargeImageGifContentView: GIFImageView, LargeImageContentView { diff --git a/Tusker/Screens/Large Image/LoadingLargeImageViewController.swift b/Tusker/Screens/Large Image/LoadingLargeImageViewController.swift index 487889b66b..1b0c2e687e 100644 --- a/Tusker/Screens/Large Image/LoadingLargeImageViewController.swift +++ b/Tusker/Screens/Large Image/LoadingLargeImageViewController.swift @@ -138,9 +138,9 @@ class LoadingLargeImageViewController: UIViewController, LargeImageAnimatableVie content = LargeImageGifContentView(gifController: gifController) } else { if let transformedImage = ImageGrayscalifier.convertIfNecessary(url: url, image: image) { - content = LargeImageImageContentView(image: transformedImage) + content = LargeImageImageContentView(image: transformedImage, owner: self) } else { - content = LargeImageImageContentView(image: image) + content = LargeImageImageContentView(image: image, owner: self) } } @@ -167,7 +167,7 @@ class LoadingLargeImageViewController: UIViewController, LargeImageAnimatableVie let grayscale = ImageGrayscalifier.convert(url: nil, cgImage: source) { image = grayscale } - setContent(LargeImageImageContentView(image: image)) + setContent(LargeImageImageContentView(image: image, owner: self)) } }