Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Capture Screenshot in Swift

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 11.0k
    Comment on it

    In order to take a screenshot of a screen and save the captured screenshot to library, use the below steps:

    1.  Add the "Privacy - Photo Library Usage Description" key to info.plist

                   

      <key>Privacy - Photo Library Usage Description</key>
      <string>YOUR CUSTOM USAGE DESCRIPTION GOES HERE.</string>

          

    2. In your controller add the below function to take screenshot:

       func captureScreenshot(){
                let layer = UIApplication.shared.keyWindow!.layer
                let scale = UIScreen.main.scale
                // Creates UIImage of same size as view
                UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
                layer.render(in: UIGraphicsGetCurrentContext()!)
                let screenshot = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                // THIS IS TO SAVE SCREENSHOT TO PHOTOS
                UIImageWriteToSavedPhotosAlbum(screenshot!, nil, nil, nil)
      }

         The first time you launch the app and call this method, the iOS device will ask you the permission to access the photos.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: