How do I take a full screen Screenshot in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In iOS, taking a full-screen screenshot usually means rendering the current window or a full-screen view hierarchy into an image. The best modern approach is to use UIGraphicsImageRenderer rather than the older image-context APIs.
What "Full Screen" Usually Means In App Code
There are two common meanings:
- capture the current app window exactly as it is displayed
- capture a specific full-screen view
This is different from capturing the entire device screen outside your app. Normal iOS app code can render your app's own UI, not arbitrary system UI from other apps.
A Modern UIView Screenshot Helper
If you already have a full-screen root view, this helper works well.
Use it like this:
This is a good default because it uses the modern renderer API and captures the visible hierarchy.
Capturing The Window
If you want the full app screen, capture the window rather than just one subview.
In a view controller:
That is usually what people mean by a full-screen screenshot inside an iOS app.
drawHierarchy Versus layer.render
You will often see two rendering approaches:
- '
drawHierarchy(in:afterScreenUpdates:)' - '
layer.render(in:)'
drawHierarchy usually gives results closer to what the user sees on screen, especially for visual effects and complex view hierarchies.
layer.render can be useful in some cases, but it may not reproduce everything exactly the way the on-screen UI looks.
Example with layer.render:
If you need a "what the user sees" capture, start with drawHierarchy.
Saving The Screenshot
Once you have a UIImage, you can save it to Photos or write it to disk.
Saving to Photos requires the appropriate privacy usage description in your app configuration.
A Full Example In A View Controller
This is enough for many in-app screenshot features, testing tools, or share flows.
What This Cannot Capture
An app can render its own UI hierarchy, but it cannot use this technique to capture system-protected content outside its own window. That means you should not think of this as a replacement for the user's hardware screenshot button combination.
It is an in-app rendering technique, not a global screen recorder.
Common Pitfalls
The most common mistake is capturing a subview when the real goal was the whole window. If you want the full app screen, render the window.
Another mistake is using older image context APIs when UIGraphicsImageRenderer is available and clearer.
Developers also get confused when layer.render does not perfectly match on-screen effects. In many cases, drawHierarchy is the better choice.
Finally, if the screenshot appears blank or incomplete, make sure the view has already laid out and appeared on screen before capturing it.
Summary
- In iOS, a full-screen in-app screenshot usually means rendering the current
UIWindow. - '
UIGraphicsImageRendereris the modern API to use.' - '
drawHierarchyusually matches the visible UI better thanlayer.render.' - Capture the window for the whole app screen or a view for a specific region.
- This technique captures your app UI, not arbitrary system content outside the app.
Related reading
- How Do I Take a Screen Shot of a UIView?
- How do I test if a string is empty in Objective-C?
- How do I upload a build to iTunes Connect for TestFlight?
- How do I use a compound drawable instead of a LinearLayout that contains an ImageView and a TextView
- How do I use a UISegmentedControl to switch views?
- How do I use custom keys with Swift 4's Decodable protocol?
- How do I use databinding to combine a string from resources with a dynamic variable in XML?
- How do I use DrawerLayout to display over the ActionBar/Toolbar and under the status bar?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.