How to take a screenshot on iOS programmatically ?

When we want to create screenshot programmatically in Ios follow the below code

if UIScreen.main.responds(to: #selector(NSDecimalNumberBehaviors.scale)) {
            UIGraphicsBeginImageContextWithOptions(self.view!.bounds.size, false, UIScreen.main.scale)
        }
        else{
            
            UIGraphicsBeginImageContext(self.view!.bounds.size)
        }
        self.view?.layer.render(in: UIGraphicsGetCurrentContext()!)
        let image : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
        UIGraphicsEndImageContext()

//save the screen shot in photos album by using this below line

        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)

And save the screenshot  in photo library then we need to add the Privacy - Photo Library Additions Usage Description in Info.plist file