Swift programming
screenshot tutorial
full screen capture
Swift UI
programming guide

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.

Browse interview questions

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.

swift
1import UIKit
2
3extension UIView {
4    func screenshot() -> UIImage {
5        let renderer = UIGraphicsImageRenderer(size: bounds.size)
6        return renderer.image { _ in
7            drawHierarchy(in: bounds, afterScreenUpdates: true)
8        }
9    }
10}

Use it like this:

swift
if let image = view.window?.screenshot() {
    print(image)
}

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.

swift
1import UIKit
2
3extension UIWindow {
4    func screenshot() -> UIImage {
5        let renderer = UIGraphicsImageRenderer(size: bounds.size)
6        return renderer.image { _ in
7            drawHierarchy(in: bounds, afterScreenUpdates: true)
8        }
9    }
10}

In a view controller:

swift
1if let window = view.window {
2    let image = window.screenshot()
3    print(image)
4}

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:

swift
1extension UIView {
2    func layerScreenshot() -> UIImage {
3        let renderer = UIGraphicsImageRenderer(size: bounds.size)
4        return renderer.image { context in
5            layer.render(in: context.cgContext)
6        }
7    }
8}

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.

swift
1if let window = view.window {
2    let image = window.screenshot()
3    UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
4}

Saving to Photos requires the appropriate privacy usage description in your app configuration.

A Full Example In A View Controller

swift
1import UIKit
2
3final class ScreenshotViewController: UIViewController {
4    @IBAction func captureTapped(_ sender: UIButton) {
5        guard let window = view.window else { return }
6        let image = window.screenshot()
7        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil)
8    }
9}

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.
  • 'UIGraphicsImageRenderer is the modern API to use.'
  • 'drawHierarchy usually matches the visible UI better than layer.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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.