Live text in gallery view

This commit is contained in:
Shadowfacts 2022-06-08 17:12:53 -04:00
parent d7f35cd1e4
commit facf039f97
2 changed files with 32 additions and 6 deletions

View File

@ -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, owner: UIViewController?) {
self.owner = owner
init(image: UIImage) {
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 {

View File

@ -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))
}
}