Trying to extend AVCaptureVidioPreviewLayer from AVFoundation to capture a screenshot of iPhone with the following swift code always return a blank white UIImage, although with the correct size. Could anyone please help what's the correct way to do this?
extension AVCaptureVideoPreviewLayer {
func screenshot() -> UIImage? {
UIGraphicsBeginImageContextWithOptions(self.bounds.size, false, 0.0)
guard let context = UIGraphicsGetCurrentContext() else { return nil }
if let connection = self.connection, connection.isVideoOrientationSupported {
let currentTransform = self.affineTransform()
context.concatenate(currentTransform)
}
self.render(in: context)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return screenshot
}
}