Last active
June 10, 2022 11:42
-
-
Save shaundon/282cf7ff276093681a1c246ae318c9d4 to your computer and use it in GitHub Desktop.
Proof of concept of using a SwiftUI `ImageRenderer` with a map, and how it blanks out the map.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import SwiftUI | |
import MapKit | |
struct ContentView: View { | |
@State private var mapRegion = MKCoordinateRegion( | |
center: CLLocationCoordinate2D( | |
latitude: 51.5, | |
longitude: -0.12 | |
), | |
span: MKCoordinateSpan( | |
latitudeDelta: 0.2, | |
longitudeDelta: 0.2 | |
) | |
) | |
@ViewBuilder | |
var map: some View { | |
ZStack { | |
Map(coordinateRegion: $mapRegion) | |
Text("Hello world") | |
.font(.title) | |
.bold() | |
.padding() | |
.background(Color.red) | |
} | |
.frame(width: 350, height: 300) | |
} | |
var body: some View { | |
VStack { | |
map | |
Button(action: { | |
let renderer = ImageRenderer(content: map) | |
if let image = renderer.cgImage { | |
// Set a breakpoint here and inspect the image that's produced. | |
// The map will be replaced with a 'no entry' sign 🚫. | |
print(image) | |
} | |
}) { | |
Text("Create image") | |
} | |
} | |
} | |
} | |
struct ContentView_Previews: PreviewProvider { | |
static var previews: some View { | |
ContentView() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment