Skip to content

Commit 5653c42

Browse files
committed
Add snapshotter example to swiftui
1 parent 92da9ae commit 5653c42

File tree

1 file changed

+35
-3
lines changed

1 file changed

+35
-3
lines changed

Apps/Examples/Examples/SwiftUI Examples/SnapshotMapExample.swift

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,60 @@
11
import SwiftUI
22
import MapboxMaps
33

4-
@available(iOS 13.0, *)
4+
@available(iOS 14.0, *)
55
struct SnapshotMapExample: View {
66
@State var image: UIImage?
77

8+
struct SnapshotterImage: Identifiable {
9+
var id: ObjectIdentifier { ObjectIdentifier(image) }
10+
let image: UIImage
11+
}
12+
@State var snapshotterImage: SnapshotterImage?
13+
@State var snapshotter = Snapshotter(options: MapSnapshotOptions(size: CGSize(width: 512, height: 512), pixelRatio: 2.0))
14+
815
var body: some View {
916
GeometryReader { geometry in
1017
VStack(spacing: 10) {
1118
MapReader { proxy in
1219
Map(initialViewport: .helsinkiOverview)
1320
.mapStyle(.outdoors)
1421
.onMapIdle { _ in image = proxy.captureSnapshot() }
22+
.onCameraChanged { event in
23+
snapshotter.setCamera(to: CameraOptions(cameraState: event.cameraState))
24+
}
1525
.frame(height: geometry.size.height / 2)
1626
}
1727

1828
SnapshotView(snapshot: image)
1929
.frame(height: geometry.size.height / 2)
2030
}
2131
}
32+
.ignoresSafeArea()
33+
.safeOverlay(alignment: .bottom) {
34+
Button("Make snapshot via Snapshotter") {
35+
snapshotter.start(overlayHandler: nil) { result in
36+
switch result {
37+
case .success(let image):
38+
self.snapshotterImage = SnapshotterImage(image: image)
39+
case .failure(let error):
40+
print("Failure: \(error)")
41+
}
42+
}
43+
}
44+
.floating()
45+
}
46+
.onAppear {
47+
snapshotter.mapStyle = .standardSatellite
48+
}
49+
.sheet(item: $snapshotterImage) {
50+
Image(uiImage: $0.image)
51+
.resizable()
52+
.scaledToFit()
53+
}
2254
}
2355
}
2456

25-
@available(iOS 13.0, *)
57+
@available(iOS 14.0, *)
2658
struct SnapshotView: View {
2759
var snapshot: UIImage?
2860

@@ -40,7 +72,7 @@ private extension Viewport {
4072
static let helsinkiOverview = Self.overview(geometry: Polygon(center: .helsinki, radius: 10000, vertices: 30))
4173
}
4274

43-
@available(iOS 13.0, *)
75+
@available(iOS 14.0, *)
4476
struct SnapshotMapExample_Preview: PreviewProvider {
4577
static var previews: some View {
4678
SnapshotMapExample()

0 commit comments

Comments
 (0)