Vorrei aggiungere alla risposta @DevC, fornendo un metodo (con blocco di completamento) che aggiunge un'annotazione nel mezzo dell'immagine. All'inizio ero interessato a vedere se potevo aggiungere un'annotazione prima che iniziasse lo snapshot, tuttavia MKMapSnapshotter
fa not support this. Quindi la soluzione che mi è venuta in mente è creare un pin, disegnarlo sopra l'immagine della mappa e quindi creare un'altra immagine della mappa + pin. Qui è il mio metodo personalizzato:
-(void)create_map_screenshot:(MKCoordinateRegion)region :(map_screenshot_completion)map_block {
// Set the map snapshot properties.
MKMapSnapshotOptions *snap_options = [[MKMapSnapshotOptions alloc] init];
snap_options.region = region;
snap_options.size = // Set the frame size e.g.: custom_view.frame.size;
snap_options.scale = [[UIScreen mainScreen] scale];
// Initialise the map snapshot camera.
MKMapSnapshotter *map_camera = [[MKMapSnapshotter alloc] initWithOptions:snap_options];
// Take a picture of the map.
[map_camera startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
// Check if the map image was created.
if ((error == nil) && (snapshot.image != nil)) {
// Create the pin image view.
MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:nil];
// Get the map image data.
UIImage *image = snapshot.image;
// Create a map + location pin image.
UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale); {
[image drawAtPoint:CGPointMake(0.0f, 0.0f)];
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
// Create the pin co-ordinate point.
CGPoint point = [snapshot pointForCoordinate:region.center];
if (CGRectContainsPoint(rect, point)) {
// Draw the pin in the middle of the map.
point.x = (point.x + pin.centerOffset.x - (pin.bounds.size.width/2.0f));
point.y = (point.y + pin.centerOffset.y - (pin.bounds.size.height/2.0f));
[pin.image drawAtPoint:point];
}
}
// Get the new map + pin image.
UIImage *map_plus_pin = UIGraphicsGetImageFromCurrentImageContext();
// Return the new image data.
UIGraphicsEndImageContext();
map_block(map_plus_pin);
}
else {
map_block(nil);
}
}];
}
Questo metodo richiede anche un colpo di testa di completamento per essere dichiarato in questo modo:
typedef void(^map_screenshot_completion)(UIImage *);
Spero che questo sia di qualche utilità per le persone in cerca di aggiungere annotazioni all'immagine mappa .
ringrazia il suo lavoro per me e mi fa risparmiare tempo. –
posso modificare l'immagine pin –