MKMapSnapshotOptions: Add a snapshot of a custom annotation viewer or UIView

I am trying to get a map display snapshot using the startWithCompletionHandler methods for MKMapSnapshotter. and I want to add an individual annotation view for snapshots. and in my custom annotation view there is a label. therefore, I can’t show this mark when I get a snapshot. here is the code:

let snapshotter = MKMapSnapshotter(options: options) snapshotter.startWithCompletionHandler() { snapshot, error in if error != nil { completion(image: nil, error: error) return } let image = snapshot.image let pin = MKPinAnnotationView(annotation: nil, reuseIdentifier: "") // I want to use custom annotation view instead of MKPinAnnotationView let pinImage = UIImage(named: "pinImage") UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale); image.drawAtPoint(CGPointMake(0, 0)) var homePoint = snapshot.pointForCoordinate(coordinates[0]) pinImage!.drawAtPoint(homePoint) pinImage!.drawAtPoint(snapshot.pointForCoordinate(coordinates[1])) let finalImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() completion(image: finalImage, error: nil) } 

as you can see drawAtPoint is a UIImage function. I try to use UIImageView, then I add a shortcut to imageView as a subView, but I cannot use drawAtPoint with imageView, so my problem is that I cannot add a shortcut to the mapView snapshot.

you can see what I mean by reference: https://www.dropbox.com/s/83hnkiqi87uy5ab/map.png?dl=0

Thanks for the advice.

+5
source share
1 answer

Create your own AnnotationView class. When you create MKMapSnapshotter, define MKPointAnnotation with the coordinate and name. After that, define the annotationView from your custom class. You can create your own annotation view with MKPointAnnotation. And use the drawViewHierarchyInRect method instead of drawAtPoint.

Your code should be like this.

  let snapshotter = MKMapSnapshotter(options: options) snapshotter.startWithCompletionHandler() { snapshot, error in if error != nil { completion(image: nil, error: error) return } let image = snapshot.image var annotation = MKPointAnnotation() annotation.coordinate = coordinates[0] annotation.title = "Your Title" let annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: "annotation") let pinImage = annotationView.image UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale); image.drawAtPoint(CGPointMake(0, 0)) //map // pinImage!.drawAtPoint(snapshot.pointForCoordinate(coordinates[0])) annotationView.drawViewHierarchyInRect(CGRectMake(snapshot.pointForCoordinate(coordinates[0]).x, snapshot.pointForCoordinate(coordinates[0]).y, annotationView.frame.size.width, annotationView.frame.size.height), afterScreenUpdates: true) let finalImage = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() completion(image: finalImage, error: nil) } 
+9
source

All Articles