Extracting Text from Images

Want to pull the text out of an image? The Vision framework's RecognizeTextRequest struct makes it straightforward.

RecognizeTextRequest performs text recognition on image data. Its perform(on:) method expects a Data value, so the first step is turning your image into Data.

UIImage provides the pngData() method, which returns the Data representation of the image.

Once you run the request, perform returns results as [RecognizedTextObservation] — the objects Vision uses to represent the text it found in the image. Each observation offers a topCandidates(_:) method that returns an array of the most likely text candidates, typed [RecognizedText].

Finally, each RecognizedText exposes its recognized value through the string property.

Putting it all together, the flow is: convert the image to Data, pass it to perform, collect the observations, and read candidate.first?.string from each one.

import Foundation
import Playgrounds
import SwiftUI
import Vision

#Playground {
    var recognizedText = ""
    var observations: [RecognizedTextObservation] = []
    var request = RecognizeTextRequest()

    let image = UIImage(resource: .stockGs200)
    if let imageData = image.pngData(),
        let results = try? await request.perform(on: imageData)
    {
        observations = results
    }

    for observation in observations {
        let candidate = observation.topCandidates(1)
        if let observedText = candidate.first?.string {
            recognizedText += "\(observedText) "
        }
    }
}