Hello,
I am struggling with an issue that contentsRect(for:) method of ImageAnalysisInteractionDelegate is not being called at any moment. I've set up the demo project where the interaction is added to root view of view controller while I'm analyzing the image of UIImageView that is added to this view. I want to achieve the behavior where I could define contents rect for highlights of found text on that image.
P.S. I know that I could simply add an interaction to an image view but that's not the case - the real work that I want to achieve in the real project is to display live text on paused video player, so that image view here is for simplicity only.
import UIKit
import VisionKit
class ViewController: UIViewController {
private let imageView = UIImageView()
private let imageAnalyzer = ImageAnalyzer()
private let interaction = ImageAnalysisInteraction()
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(imageView)
imageView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
imageView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
imageView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
imageView.centerYAnchor.constraint(equalTo: view.centerYAnchor),
imageView.heightAnchor.constraint(equalTo: view.heightAnchor, multiplier: 0.7)
])
interaction.delegate = self
// Some image with text that I have in assets
imageView.image = UIImage(named: "IMG_5564")
imageView.contentMode = .scaleAspectFit
view.addInteraction(interaction)
interaction.setContentsRectNeedsUpdate()
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.analyze()
}
}
private func analyze() {
Task {
let imageAnalysis = try? await imageAnalyzer.analyze(
self.imageView.image!,
configuration: .init([.machineReadableCode, .text])
)
self.interaction.analysis = imageAnalysis
self.interaction.preferredInteractionTypes = .automatic
self.interaction.setContentsRectNeedsUpdate()
}
}
}
extension ViewController: ImageAnalysisInteractionDelegate {
func presentingViewController(for interaction: ImageAnalysisInteraction) -> UIViewController? {
return nil
}
func contentsRect(for interaction: ImageAnalysisInteraction) -> CGRect {
// >>> This method is never being called <<<
return CGRect(x: 0, y: 0, width: 1.0, height: 0.7)
}
func contentView(for interaction: ImageAnalysisInteraction) -> UIView? {
return nil
}
func interaction(_ interaction: ImageAnalysisInteraction, highlightSelectedItemsDidChange highlightSelectedItems: Bool) {
debugPrint("highlight: \(highlightSelectedItems)")
}
}
Hello,
Please file a bug report for this issue using Feedback Assistant if you have not already!