Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I take a full screen Screenshot in Swift?

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?

like image 942
Pietro La Spada Avatar asked Aug 22 '14 14:08

Pietro La Spada


People also ask

How do I capture full screen on iPhone?

Save a full-page screenshot as a PDF Do one of the following: On an iPhone with Face ID: Simultaneously press and then release the side button and volume up button. On an iPhone with a Home button: Simultaneously press and then release the Home button and the side button or Sleep/Wake button (depending on your model).

How do you screenshot a Mac?

How to take a screenshot on your Mac. To take a screenshot, press and hold these three keys together: Shift, Command, and 3. If you see a thumbnail in the corner of your screen, click it to edit the screenshot. Or wait for the screenshot to save to your desktop.

Can we take screenshot in UiPath?

UiPath has an inbuilt Take Screenshot activity in UiPath. UIAutomation. Activities package. This activity can be used to take a screenshot of a specified UI element or a region.


1 Answers

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.

UIGraphicsBeginImageContext(view.frame.size) 

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 as view. Unless you want to capture a low resolution (non-retina) version of your application, you should probably be using UIGraphicsBeginImageContextWithOptions instead. Then you can pass 0.0 to get the same scale factor as the devices main screen.

view.layer.renderInContext(UIGraphicsGetCurrentContext()) 

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.

let image = UIGraphicsGetImageFromCurrentImageContext() 

This line of code creates an UIImage object from what has been drawn into the graphics context.

UIGraphicsEndImageContext() 

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, with view 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 of UIView, it can be drawn into a image context as well.

like image 180
David Rönnqvist Avatar answered Sep 21 '22 03:09

David Rönnqvist