-1

The viewcontroller i am taking a screenshot from is set to "over current context" therefore showing an image and info in the view controller under it.

When i however take a screenshot programatically the screen turns black where it is supposed to be transparent therefore showing the viewcontroller under it.

how can this be solved?

let bounds = UIScreen.main.bounds
    UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
    self.view.drawHierarchy(in: bounds, afterScreenUpdates: false)
    let image = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return image!

My code (Currently returning a black screen where id like it to be transparent showing Viewcontroller under)

Redstain
  • 157
  • 2
  • 10

1 Answers1

0

try to this code it's working good :


func screenShotMethod() {

        let bounds = UIScreen.main.bounds
        UIGraphicsBeginImageContextWithOptions(bounds.size, true, 0.0)
        // myView : change with your view
        myView.drawHierarchy(in: bounds, afterScreenUpdates: false)

        let img = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        UIImageWriteToSavedPhotosAlbum(img!, nil, nil, nil)

    }




GHAZZWAY
  • 13
  • 5
  • Thanks for answering. However it still remains black(not showing viewcontroller under) – Redstain Sep 05 '19 at 12:21
  • The code posted here solved my problem. https://stackoverflow.com/questions/41308685/screenshot-on-swift-programmatically – Redstain Sep 05 '19 at 12:24