![]() / The default value of scale property is 1. (Credit: PCMag) Press the PrtSc key (sometimes spelled out as Print Screen) and the full desktop image copies to the clipboard. Let renderer = ImageRenderer(content: createSampleView(number: randomNumber)) Use the PrtSc (Print Screen) Key and Paste. ![]() struct ContentView: View private var renderedImage = Image(systemName: var displayScale However, in this example, because it is firing from the Button action which is always on the main thread the MainActor is unnecessary. Just keep in mind, you have to call it on the main thread. I simply needed a function to get the view's screen capture, so I hope this helps anyone looking for the sameĪfter iOS 16 you can use ImageRenderer to export bitmap image data from a SwiftUI view. To include the nav bar and other extras, call this method off of the window instead of the view. I pieced together this answer from taking the code in the question and following David Rönnqvist's suggestions (thank you for the explanation), with some tweaks. clean up newly created context and return screenshot Let screenshot: UIImage? = UIGraphicsGetImageFromCurrentImageContext() creates UIImage from what was drawn into graphics context Open func takeScreenshot(_ shouldSave: Bool = true) -> UIImage? / - Returns: (Optional)image captured as a screenshot / - Parameter shouldSave: Boolean flag asking if the image needs to be saved to user's photo library. Swift 4 /// Takes the screenshot of the screen and returns the corresponding image Since UIWindow is a subclass of UIView, it can be drawn into a image context as well. In practice, you are likely just talking about everything in the "key window" of your application. 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. The result is an image that is the same size as view, with view and its subviews drawn into it. It's clean up (you created the context and should remove it as well. This line of code ends the image context. This line of code creates an UIImage object from what has been drawn into the graphics context. let image = UIGraphicsGetImageFromCurrentImageContext() 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 will render the view's layer into the current graphics context (which is the context you just created). Then you can pass 0.0 to get the same scale factor as the devices main screen. Unless you want to capture a low resolution (non-retina) version of your application, you should probably be using UIGraphicsBeginImageContextWithOptions instead. The main thing to take away here is that the new image context is the same size as view. This line of code creates a new image context with the same size as view. 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.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |