I'm trying to draw an annotation exactly as in a 'live' MapView, but then in a MKSnapshot. Why an MKSnapshot > because I want to have non-interactive MapViews in a UITableView and using images is more efficient.
I can get a pin to be drawn (though not a point as in iOS 11, the pin looks old) using MKPinAnnotationView, but also then there is no title of the annotation on the image. Using almost exactly this code: Snapshot of MKMapView in iOS7.
You can use the following steps:
with MKMapSnapshotter you will get an image of the map without annotations
you can retrieve the annotations from your MKMapView
for each annotations determine its position in the coordinate space of the image
draw a custom pin there (may look like Apple's pins)
determine the text and size of the annotation title and draw it centered below the position of the pin
The result can look very similar to what MKMapView displays. In the attached screenshot there is a MKMapView in the upper area and an UIImageView with the resulting image in the lower area. Looks similar, doesn't it?
Here the Swift 4 code for the screenshot above:
Alternative
If you prefer to draw a MKMarkerAnnotationView (e.g. to get the nice shadow for free) you can change the
drawPin
to this:Don't forget to change the call to
The result looks like this then: