Instead of just discarding the answer, let me explain what your current code does and how to modify it to capture the entire screen.
UIGraphicsBeginImageContext(view.frame.size)
This line of code creates a new image context with the same size as view . The main thing to remove here is that the new image context is the same size as the view . If you do not want to grab a low-resolution version of the application (without the retina), chances are you should use UIGraphicsBeginImageContextWithOptions . You can then transfer 0.0 to get the same scale factor as the main screen of the device.
view.layer.renderInContext(UIGraphicsGetCurrentContext())
This line of code will map the view layer to the current graphics context (which is the context just created). The main thing to clean up here is that only view (and its sub-items) are drawn into the image context.
let image = UIGraphicsGetImageFromCurrentImageContext()
This line of code creates a UIImage object from what was drawn in the graphics context.
UIGraphicsEndImageContext()
This line of code ends the image context. It is cleared (you created the context and must delete it.
The result is an image with the same size as view , with view and its captions embedded in it.
If you want to draw everything on the image, then you must create an image that is the size of the screen and draw everything on the screen. In practice, you're probably just talking about everything in the "key window" of your application. Because UIWindow is a subclass of UIView , it can also be inserted into the image context.
David Rรถnnqvist Aug 22 '14 at 2:26 a.m. 2014-08-22 14:26
source share