1

I have an app with UIView showing quoted text. A save button creates an image of the view using UIGraphicsBeginImageContextWithOptions. This image is then shared using the Share Sheet or saved into the user's camera roll.

I want to be able to provide VoiceOver support to read the text on the image / or the text that I give should be read.

Since the generated image is not a part of the shown view, I used initWithAccessibilityContainer. Used instructions on this post and this.

This is the code that gets called on button click :

    @IBAction func onButtonClick(_ sender: UIButton) {
    
    guard let resizedImage = generateImage(from: view) else {
        fatalError("No image created")
    }
    
    let imageRect = CGRect(x: 0, y: 0, width: resizedImage.size.width, height: resizedImage.size.height)
    resizedImage.isAccessibilityElement = true
    resizedImage.accessibilityLabel = "Image created"
    resizedImage.accessibilityTraits = .image
   
    let groupedElement = UIAccessibilityElement(accessibilityContainer: self.view)
    groupedElement.accessibilityLabel = "This is the description that i want to be read to voiceOver enabled users"
    groupedElement.accessibilityFrameInContainerSpace = imageRect
    groupedElement.accessibilityFrame = self.view.convert(imageRect, to: nil)
    
    var elements = [UIAccessibilityElement]()
    elements.append(groupedElement)
    
    accessibilityElements = [elements]
    
    UIImageWriteToSavedPhotosAlbum(resizedImage, self, #selector(onImageSaved(_:didFinishSavingWithError:contextInfo:)), nil)
    
}`

But when I save the image and go to my camera roll and check the label through Accessibility Inspector I get the "Photo, Landscape, 3:28PM" only being read for the image. I expect this "This is the description that I want to be read to voiceOver enabled users".

I read on Apple's site that VoiceOver can "It can also read aloud text in an image — whether it’s a snapshot of a receipt or a magazine article — even if it hasn’t been annotated."

Could someone please let me know if the technique I am following is the right one? And what changes need to be done to get the required text on VoiceOver?

Kavisha
  • 155
  • 1
  • 10

0 Answers0