MKMarkerAnnotationView being clipped

I am trying to create a map snapshot, using MKMapSnapshotter that includes a MKMarkerAnnotationView. The issue I am facing is that the MKMarkerAnnotationView is being clipped.

I can see that the frame of my MKMarkerAnnotationView is 28*28, which seems to be too small.

My code:

Code Block swift
if let image = snapshot?.image {
/* Create the annotation */
    let annotation = MKPointAnnotation()
    annotation.coordinate = center
/* Create the annotation view */
    let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: nil)
    annotationView.glyphImage = UIImage(named: "mappin")
    annotationView.isSelected = true
/* annotationView.frame would show the width and height being 28 */
/* Start the context */
    UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale)
/* Draw the image from the MKMapSnapshotter in the context */
    image.draw(at: CGPoint(x: 0, y: 0))
/* Define the rect where the Annotation View should be rendered in */
    let rect = CGRect(x: snapshot!.point(for: center).x, y: snapshot!.point(for: center).y, width: annotationView.frame.width, height: annotationView.frame.height)
/* Draw the Annotation View in the context */
    annotationView.drawHierarchy(in: rect, afterScreenUpdates: true)
    let finalImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
}


I know this was two years ago so you probably found a workaround by now, but I just came across this and figured out a hack to make it work.

// Manually set the bounds to be larger than 28x28.
// 80 is an arbitrary number to prevent clipping.
annotationView.bounds = CGRect(
  x: self.point(for: location.coordinate).x,
  y: self.point(for: location.coordinate).y,
  width: 80,
  height: 80
)

// Use the bounds (not the frame) with setting the rect to draw in
let rect = CGRect(
  x: self.point(for: location.coordinate).x,
  y: self.point(for: location.coordinate).y,
  width: annotationView.bounds.width,
  height: annotationView.bounds.height
)

For me, this has fixed the problem. I'm not completely comfortable with having to use 80 as an arbitrary value – it feels like it could break in the future – but it'll do for now.

MKMarkerAnnotationView being clipped
 
 
Q