Convert a screen capture into a UIImage UIImagePNG

2019-09-14 21:23发布

问题:

Below I convert a screen capture into a NSData UIImagePNGRepresentation.

But how do I correct the below code and convert a screen capture into a UIImagePNGRepresentation but ensure it's using a UIImage format and not NSData?

// Take snapshot of screen
var imageSnapshot: UIImage!
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, false, 0)
self.view.drawViewHierarchyInRect(CGRect(x: 0, y: 0, width: self.view.bounds.width, height: self.view.bounds.height), afterScreenUpdates: false)
imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

// Prepare image to PNG format
let imageShare: NSData
imageShare = UIImagePNGRepresentation(imageSnapshot)!