Skip to content

Instantly share code, notes, and snippets.

@shaundon
Last active June 10, 2022 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaundon/282cf7ff276093681a1c246ae318c9d4 to your computer and use it in GitHub Desktop.
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.
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