I've found this code:
func screenShotMethod() {
//Create the UIImage
UIGraphicsBeginImageContext(view.frame.size)
view.layer.renderInContext(UIGraphicsGetCurrentContext())
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
//Save it to the camera roll
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
}
What do I need to do to get all the other elements, like the navigation bar, into the screenshots?
I use this method:
It captures all but the status bar, and it doesn't ask for permission to save images in the camera roll.
Hope it helps!
Swift 3 example:
My version also capture a keyboard. Swift 4.2
Instead of just throwing the answer out there, let me explain what your current code does and how to modify it to capture the full screen.
This line of code creates a new image context with the same size as
view
. The main thing to take away here is that the new image context is the same size asview
. Unless you want to capture a low resolution (non-retina) version of your application, you should probably be usingUIGraphicsBeginImageContextWithOptions
instead. Then you can pass0.0
to get the same scale factor as the devices main screen.This line of code will render the view's layer into the current graphics context (which is the context you just created). The main thing to take away here is that only
view
(and its subviews) are being drawn into the image context.This line of code creates an UIImage object from what has been drawn into the graphics context.
This line of code ends the image context. It's clean up (you created the context and should remove it as well.
The result is an image that is the same size as
view
, withview
and its subviews drawn into it.If you want to draw everything into the image, then you should create an image that is the size of the screen and draw everything that is on screen into it. In practice, you are likely just talking about everything in the "key window" of your application. Since
UIWindow
is a subclass ofUIView
, it can be drawn into a image context as well.Swift 4
Updated for Swift 2
The code you provide works but doesn't allow you to capture the
NavigationBar
and theStatusBar
in your screenshot. If you want to take a screenshot of your device that will include theNavigationBar
, you have to use the following code:With this code:
StatusBar
will not appear in the final image.