Dunfey · Hotel WWDC as data, est. 1983
Front desk everything
Years
Topics

2022 Photos & CameraApp Services

WWDC22 · 14 min · Photos & Camera / App Services

Add Live Text interaction to your app

Learn how you can bring Live Text support for still photos or paused video frames to your app. We’ll share how you can easily enable text interactions, translation, data detection, and QR code scanning within any image view on iOS, iPadOS, or macOS. We’ll also go over how to control interaction types, manage the supplementary interface, and resolve potential gesture conflicts. To learn more about capturing and interacting with detected data in live camera feeds, watch "Capture machine-readable codes and text with VisionKit" from WWDC22.

Watch at developer.apple.com ↗

Transcript all transcripts

Code shown on screen · 1 snippet

Live Text Sample Adoption swift · at 2:37 ↗
import UIKit
import VisionKit

class LiveTextDemoController: BaseController, ImageAnalysisInteractionDelegate, UIGestureRecognizerDelegate {
   
    let analyzer = ImageAnalyzer()
    let interaction = ImageAnalysisInteraction()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        imageview.addInteraction(interaction)
    }
    
    override var image: UIImage? {
        didSet {
            interaction.preferredInteractionTypes = []
            interaction.analysis = nil
            analyzeCurrentImage()
        }
    }
    
    func analyzeCurrentImage() {
        if let image = image {
            Task {
               let configuration = ImageAnalyzer.Configuration([.text, .machineReadableCode])
                do {
                    let analysis = try await analyzer.analyze(image, configuration: configuration)
                    if let analysis = analysis, image == self.image {
                        interaction.analysis = analysis;
                        interaction.preferredInteractionTypes = .automatic
                    }
                }
                catch {
                    // Handle error…
                }
            }
        }
    }
}

Resources