You have two options: First, you can take a screenshot using UIApplication.sharedApplication().keyWindow , then we get the following:
UIGraphicsBeginImageContext(UIApplication.sharedApplication().keyWindow!.bounds.size) UIApplication.sharedApplication().keyWindow!.layer.renderInContext(UIGraphicsGetCurrentContext()) let screenshot = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()
So you need to crop the image as follows:
let yPosition = self.navigationController!.navigationBar.frame.height + UIApplication.sharedApplication().statusBarFrame.size.height let crop = CGRectMake(0, yPosition, self.view.bounds.width, self.view.bounds.height) let cgImage = CGImageCreateWithImageInRect(screenshot.CGImage, crop) let image: UIImage = UIImage(CGImage: cgImage)!
The second option is to take a screenshot directly from your view, for example:
UIGraphicsBeginImageContext(self.view!.bounds.size) self.view!.layer.renderInContext(UIGraphicsGetCurrentContext()) let screenshot = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext()
In this case, you do not need to trim the navigation bar.
This is sample code on github https://github.com/gazolla/crop
source share