I have a live text implementation on the following LiveTextImageView. However, after the view loads and the analyze code is run, none of the delegate methods fire when I interact with the Live View. Selecting text does not fire the textSelectionDidChange method, nor does highlightSelectedItemsDidChange fire when the live text button in the bottom right is pressed. I tried a few different implementations, including an approach where the delegate was defined on a separate class. I am running this on a iPhone 12 Pro I recently updated to 17.0.3.
My goal is to be able to provide additional options to the user beyond the default live-text overlay options, after identifying when they have finished selecting text.
//
// LiveTextImageView.swift
//
import UIKit
import SwiftUI
import VisionKit
class ImageAnalyzerWrapper {
static let shared = ImageAnalyzer()
private init() { }
}
struct LiveTextImageViewRepresentable: UIViewRepresentable {
var image: UIImage
func makeUIView(context: Context) -> LiveTextImageView {
return LiveTextImageView(image: image)
}
func updateUIView(_ uiView: LiveTextImageView, context: Context) { }
}
class LiveTextImageView: UIImageView, ImageAnalysisInteractionDelegate, UIGestureRecognizerDelegate {
var capturedSelectedText: String?
let analyzer = ImageAnalyzerWrapper.shared
let interaction = ImageAnalysisInteraction()
init(image: UIImage) {
super.init(frame: .zero)
self.image = image
let photoWrapper = PhotoWrapper(rawPhoto: image)
let resizedPhoto = photoWrapper.viewportWidthCroppedPhoto(padding: 40)
self.image = resizedPhoto
self.contentMode = .scaleAspectFit
self.addInteraction(interaction)
interaction.preferredInteractionTypes = []
interaction.analysis = nil
analyzeImage()
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func analyzeImage() {
if let image = self.image {
Task {
let configuration = ImageAnalyzer.Configuration([.text])
do {
let analysis = try await analyzer.analyze(image, configuration: configuration)
self.addInteraction(interaction)
interaction.delegate = self
interaction.analysis = analysis
interaction.preferredInteractionTypes = .textSelection
}
catch {
print("Error in live image handling")
}
}
}
}
func interaction(
_ interaction: ImageAnalysisInteraction,
highlightSelectedItemsDidChange highlightSelectedItems: Bool) async {
print("Highlighted items changed")
}
func interaction(_ interaction: ImageAnalysisInteraction, shouldBeginAt point: CGPoint, for interactionType: ImageAnalysisInteraction.InteractionTypes) async -> Bool {
return interaction.hasInteractiveItem(at: point) || interaction.hasActiveTextSelection
}
func textSelectionDidChange(_ interaction: ImageAnalysisInteraction) async {
print("Changed!")
if #available(iOS 17.0, *) {
capturedSelectedText = interaction.text
print(capturedSelectedText ?? "")
}
}
}