0

In an attempt to capture a screenshot, everything but the UILabels is being captured. I've checked the view hierarchy to see if the labels were being hidden somehow but everything looks fine.

By the way, I am happy that the navigation controller is hidden in the screenshot, I just need to figure out why the labels are hidden.

I am not sure where I am going wrong.

@IBAction func cameraBarButtonItem(_ sender: UIBarButtonItem) {

  UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)

  guard let context = UIGraphicsGetCurrentContext() else {
    print("No Graphics Context")
    return
  }

  view.layer.render(in: context)

  let image = UIGraphicsGetImageFromCurrentImageContext()

  UIGraphicsEndImageContext()

  print("Photo Screen Captured")

  guard let photoImage = image else {
    return
  }

  guard let imageData = UIImageJPEGRepresentation(photoImage, 1.0) else {
    return
  }

  userQueue.async {

    savePhotoToPhotoLibrary(data: imageData) {

    }
  }
}

enter image description here enter image description here

enter image description here

lifewithelliott
  • 1,012
  • 2
  • 16
  • 35

2 Answers2

0
UIGraphicsBeginImageContextWithOptions(view.bounds.size, false, UIScreen.main.scale)

  guard let context = UIGraphicsGetCurrentContext() else {
    print("No Graphics Context")
    return
  }

  view.layer.render(in: context)

Make sure the UILabel is a subview of the view that you are using for the screenshot.

Otherwise, use this code from another SO post to take a screenshot of the entire screen.

shim
  • 9,289
  • 12
  • 69
  • 108
Kishore Kumar
  • 4,265
  • 3
  • 26
  • 47
0

The method that seems to do the trick is:

self.view.drawViewHierarchyInRect(view.bounds, afterScreenUpdates: true)
rmaddy
  • 314,917
  • 42
  • 532
  • 579
lifewithelliott
  • 1,012
  • 2
  • 16
  • 35