-
-
Save azcoov/ff163abced922a36c139bd298bb87db7 to your computer and use it in GitHub Desktop.
MKMapSnapshotter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)setupMapSnapshot | |
{ | |
CLLocationCoordinate2D coordinate = self.outlet.annotaion.coordinate; | |
MKMapSnapshotOptions* options = [MKMapSnapshotOptions new]; | |
options.size = self.mapImageView.frame.size; | |
options.scale = [[UIScreen mainScreen] scale]; | |
options.region = MKCoordinateRegionMakeWithDistance(coordinate, 2000.f, 2000.f); | |
MKMapSnapshotter* snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options]; | |
[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) | |
completionHandler:^(MKMapSnapshot *snapshot, NSError *error) { | |
if (error) { | |
NSLog(@"MKMapSnapshotter error: %@", error); | |
return ; | |
} | |
// draw annotaion pin | |
UIImage *image = snapshot.image; | |
snapshot = nil; | |
MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:@""]; | |
UIImage *pinImage = pin.image; | |
UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale); | |
[image drawAtPoint:CGPointMake(0, 0)]; | |
CGPoint point = [snapshot pointForCoordinate:coordinate]; | |
CGPoint pinCenterOffset = pin.centerOffset; | |
point.x -= pin.bounds.size.width / 2.0; | |
point.y -= pin.bounds.size.height / 2.0; | |
point.x += pinCenterOffset.x; | |
point.y += pinCenterOffset.y; | |
[pinImage drawAtPoint:CGPointMake(image.size.width/2, image.size.height/2)]; | |
__block UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); | |
UIGraphicsEndImageContext(); | |
image = nil; | |
dispatch_async(dispatch_get_main_queue(), ^ { | |
self.mapImageView.image = finalImage; | |
finalImage = nil; | |
}); | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment