<!--
{
  "availability" : [
    "Xcode: 16.3.0 -",
    "iOS: -",
    "iPadOS: -",
    "macCatalyst: -",
    "macOS: -",
    "tvOS: -",
    "visionOS: -",
    "watchOS: -"
  ],
  "documentType" : "symbol",
  "framework" : "XCUIAutomation",
  "identifier" : "/documentation/XCUIAutomation/XCUIScreenshot",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "XCUIAutomation"
    ],
    "preciseIdentifier" : "c:objc(cs)XCUIScreenshot"
  },
  "title" : "XCUIScreenshot"
}
-->

# XCUIScreenshot

A captured image of a screen, app, or UI element state.

```
@MainActor class XCUIScreenshot
```

## Overview

Screenshots capture the current UI state of classes that conform to the [`XCUIScreenshotProviding`](/documentation/XCUIAutomation/XCUIScreenshotProviding) protocol, such as [`XCUIScreen`](/documentation/XCUIAutomation/XCUIScreen) and [`XCUIElement`](/documentation/XCUIAutomation/XCUIElement). Each screenshot contains an image representation of the captured UI at the point the screenshot was taken.

The following code demonstrates taking screenshots of a screen and a UI element:

```swift
func testTakeScreenshots() {

    // Take a screenshot of the current device's main screen.
    let mainScreenScreenshot = XCUIScreen.main.screenshot()
    
    // Take a screenshot of an app's first window.
    let app = XCUIApplication()
    app.launch()
    let windowScreenshot = app.windows.firstMatch.screenshot()

}
```

If you use <doc://com.apple.documentation/documentation/XCTest> for your UI automation tests, you can attach a screenshot of your app’s UI to a test or activity to store it for later analysis. Create an attachment for a screenshot by calling the <doc://com.apple.documentation/documentation/XCTest/XCTAttachment> initializer <doc://com.apple.documentation/documentation/XCTest/XCTAttachment/init(screenshot:)> or <doc://com.apple.documentation/documentation/XCTest/XCTAttachment/init(screenshot:quality:)>. Add the attachment to a test or activity by calling the <doc://com.apple.documentation/documentation/XCTest/XCTActivity> method <doc://com.apple.documentation/documentation/XCTest/XCTActivity/add(_:)>. For more information, see <doc://com.apple.documentation/documentation/XCTest/adding-attachments-to-tests-activities-and-issues>.

## Topics

### Screenshot representations

[`image`](/documentation/XCUIAutomation/XCUIScreenshot/image)

A representation of the screenshot as a platform-native image object.

[`pngRepresentation`](/documentation/XCUIAutomation/XCUIScreenshot/pngRepresentation)

A representation of the screenshot as PNG image data.



---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)