问题
I'm using this function I've found in this site but it saves the whole screen with the navigation bar but no the status bar.
func screenShotMethod() {
let layer = UIApplication.sharedApplication().keyWindow!.layer
let scale = UIScreen.mainScreen().scale
UIGraphicsBeginImageContextWithOptions(layer.frame.size, false, scale);
layer.renderInContext(UIGraphicsGetCurrentContext()!)
let screenshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
UIImageWriteToSavedPhotosAlbum(screenshot, nil, nil, nil)
let alertSaved = UIAlertController(title: "Saved", message: "", preferredStyle: .ActionSheet)
alertSaved.addAction(UIAlertAction(title: "Ok", style: .Default, handler: {action in
self.view.removeGestureRecognizer(self.gesture)
}))
presentViewController(alertSaved, animated: true, completion: nil)
}
What I need is just take a snapshot from the end of the navigation bar to the bottom.
回答1:
If you change the layer you are attempting to screenshot from the keyWindow's view to your view controller's view, the navigation bar (assuming you have your view controller embedded in a navigation controller rather than adding a nav bar as a subview of your view) and the status bar will be omitted from the screenshot.
let layer = view.layer
If you have blank space at the top where the navigation bar was, you can adjust the frame to cut this out.
来源:https://stackoverflow.com/questions/34077806/how-do-i-make-a-snapshot-without-the-navigation-bar-in-ios-with-swift