In order to take a screenshot of a screen and save the captured screenshot to library, use the below steps:
- 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)