Swift
Screenshot
Full Screen
iOS Development
Programming Tutorial

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

Capturing full-screen screenshots in Swift is useful for support flows, visual regression testing, and user feedback features. The main technical challenge is selecting the correct window in multi-scene iOS apps and capturing at the right moment in the UI lifecycle. A production-quality implementation should also handle permissions, compression, and privacy safeguards.

Capture Active Key Window with Renderer

The most reliable app-level approach uses UIGraphicsImageRenderer on the active key window.

swift
1import UIKit
2
3func captureFullScreenImage() -> UIImage? {
4    guard let windowScene = UIApplication.shared.connectedScenes
5        .compactMap({ $0 as? UIWindowScene })
6        .first,
7          let window = windowScene.windows.first(where: { $0.isKeyWindow }) else {
8        return nil
9    }
10
11    let renderer = UIGraphicsImageRenderer(bounds: window.bounds)
12    let image = renderer.image { _ in
13        window.drawHierarchy(in: window.bounds, afterScreenUpdates: true)
14    }
15
16    return image
17}

This captures your app UI content, not protected system overlays.

Capture Timing Matters

Capturing during animation or transition can produce partial frames. Trigger after layout settles.

swift
1DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
2    if let image = captureFullScreenImage() {
3        print("Captured size: \(image.size)")
4    }
5}

Use minimal delay needed for the current screen flow. Too long can capture unintended UI state.

Save Screenshot to Photo Library

If screenshot should persist, request add-only permission and write image.

swift
1import UIKit
2import Photos
3
4func saveToPhotos(_ image: UIImage, completion: @escaping (Bool, Error?) -> Void) {
5    PHPhotoLibrary.requestAuthorization(for: .addOnly) { status in
6        guard status == .authorized || status == .limited else {
7            completion(false, nil)
8            return
9        }
10
11        PHPhotoLibrary.shared().performChanges({
12            PHAssetChangeRequest.creationRequestForAsset(from: image)
13        }, completionHandler: completion)
14    }
15}

Remember to include required photo usage description keys in app configuration.

Share Screenshot Without Saving

For quick support reporting, sharing directly is often better than persisting to Photos.

swift
1import UIKit
2
3func presentShareSheet(image: UIImage, from controller: UIViewController) {
4    let activity = UIActivityViewController(activityItems: [image], applicationActivities: nil)
5    controller.present(activity, animated: true)
6}

This avoids storage overhead and permission prompts in many workflows.

Compress Before Uploading to Backend

Raw screenshots can be large. Compress to reduce upload time and mobile data usage.

swift
1if let screenshot = captureFullScreenImage(),
2   let jpeg = screenshot.jpegData(compressionQuality: 0.75) {
3    print("Upload payload bytes: \(jpeg.count)")
4}

Tune compression quality to balance readability and transfer size.

Multi-Scene and Window Ownership Considerations

Apps with multiple scenes, external displays, or overlays should avoid global first-scene assumptions when possible. Passing explicit window from calling context is safer.

Example approach:

  • Caller provides current UIWindow or hosting controller.
  • Screenshot utility renders from that exact window.
  • Result is tied to known UI context.

This reduces risk of capturing wrong scene in split-view or multitasking setups.

Full Scroll Content Versus Visible Screen

Full-screen screenshot captures only visible viewport. For long scroll views, you need stitched capture logic.

Typical stitched strategy:

  • Save current content offset.
  • Render visible segments sequentially.
  • Compose segments into one large canvas.
  • Restore original offset.

This is more complex and should be used only when full-content capture is truly required.

Privacy and Security Controls

Screenshots may include sensitive data such as personal info, payment details, or tokens. Add safeguards:

  • Mask sensitive fields before capture.
  • Encrypt uploads in transit.
  • Enforce retention limits on stored images.
  • Restrict screenshot access in support tools.

Treat screenshots as user data, not harmless diagnostics.

Snapshot Testing Use Case

Automated screenshot testing benefits from deterministic capture conditions:

  • Fixed locale and region.
  • Fixed text size and appearance mode.
  • Stable test data.
  • Disabled dynamic timestamps where possible.

Deterministic setup reduces flaky visual diffs and improves confidence in UI regression checks.

Common Pitfalls

  • Capturing from wrong window in multi-scene environments.
  • Taking screenshot before UI settles and getting partial render.
  • Ignoring permission flow when saving to photo library.
  • Uploading uncompressed images and causing slow or failed transfers.
  • Storing screenshots without privacy controls or retention policy.

Summary

  • Use UIGraphicsImageRenderer against active key window for reliable app-level screenshots.
  • Capture after screen updates to avoid incomplete frames.
  • Choose share or save flow based on product needs.
  • Compress screenshots before upload for better performance.
  • Handle multi-scene ownership explicitly in complex apps.
  • Apply privacy safeguards because screenshots often contain sensitive user data.

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.