Skip to content

Take screenshot

View on GitHub

Take a screenshot of the map.

Image of Take screenshot sample

Use case

GIS users may want to export a screenshot of a map to enable sharing as an image or printing.

How to use the sample

Pan and zoom to find an interesting location, then use the button to take a screenshot. The screenshot will be displayed. You can share the screenshot by tapping the share button.

How it works

  1. Wait for the MapView to finish drawing.
  2. Call exportImage() to get a UIImage.

Relevant API

  • DrawStatus
  • GeoViewProxy.exportImage()

Tags

capture, export, image, print, screen capture, screenshot, share, shot

Sample Code

TakeScreenshotView.swift
Use dark colors for code blocksCopy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
// Copyright 2025 Esri
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//   https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import ArcGIS
import SwiftUI

struct TakeScreenshotView: View {
    /// The current draw status of the map.
    @State private var currentDrawStatus: DrawStatus = .inProgress
    /// The screenshot to export.
    @State private var screenshot: Screenshot?
    /// The map with an imagery basemap centered on Hawaii.
    @State private var map: Map = {
        let map = Map(basemapStyle: .arcGISImageryStandard)
        map.initialViewpoint = Viewpoint(
            latitude: 20.78,
            longitude: -156.84,
            scale: 5_000_000
        )
        return map
    }()

    var body: some View {
        MapViewReader { mapViewProxy in
            MapView(map: map)
                .onDrawStatusChanged { drawStatus in
                    withAnimation {
                        currentDrawStatus = drawStatus
                    }
                }
                .sheet(item: $screenshot) { screenshot in
                    NavigationStack {
                        ShareScreenshotView(screenshot: screenshot)
                    }
                }
                .toolbar {
                    ToolbarItem(placement: .bottomBar) {
                        Button {
                            Task {
                                // The map view proxy is used to export a
                                // screenshot of the map view.
                                let image = try await mapViewProxy.exportImage()
                                screenshot = Screenshot(
                                    image: Image(uiImage: image),
                                    caption: "A screenshot of the map."
                                )
                            }
                        } label: {
                            if currentDrawStatus != .completed {
                                ProgressView()
                                    .progressViewStyle(.circular)
                            }
                            Text("Take Screenshot")
                        }
                        .disabled(currentDrawStatus != .completed)
                    }
                }
        }
    }
}

private extension TakeScreenshotView {
    /// A struct that represents a screenshot with a caption to be shared.
    struct Screenshot: Transferable, Identifiable {
        static var transferRepresentation: some TransferRepresentation {
            ProxyRepresentation(exporting: \.image)
        }

        let id = UUID()
        let image: Image
        let caption: String
    }

    /// A view that displays an image to be shared.
    struct ShareScreenshotView: View {
        /// The action to dismiss the sheet.
        @Environment(\.dismiss) private var dismiss: DismissAction
        /// The screenshot to display.
        let screenshot: Screenshot

        var body: some View {
            screenshot.image
                .resizable()
                .aspectRatio(contentMode: .fit)
                .toolbar {
                    ToolbarItem(placement: .confirmationAction) {
                        Button("Done") {
                            dismiss()
                        }
                    }

                    ToolbarItem(placement: .navigationBarTrailing) {
                        // A share link to share the screenshot image.
                        ShareLink(
                            item: screenshot,
                            preview: SharePreview(
                                screenshot.caption,
                                image: screenshot.image
                            )
                        )
                    }
                }
        }
    }
}

#Preview {
    TakeScreenshotView()
}

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.