[ASMapNode] Perform MKMapSnapshotter callback off the main thread to eliminate UIImage handling overhead.

This commit is contained in:
Scott Goodson 2016-02-24 17:02:22 -08:00
parent 306aec9d5b
commit 792db0061b

View File

@ -160,36 +160,42 @@
[self setUpSnapshotter]; [self setUpSnapshotter];
} }
[_snapshotter cancel]; [_snapshotter cancel];
[_snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) { [_snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (!error) { if (!error) {
UIImage *image = snapshot.image; UIImage *image = snapshot.image;
if (_annotations.count > 0) {
// Only create a graphics context if we have annotations to draw.
// The MKMapSnapshotter is currently not capable of rendering annotations automatically.
CGRect finalImageRect = CGRectMake(0, 0, image.size.width, image.size.height); CGRect finalImageRect = CGRectMake(0, 0, image.size.width, image.size.height);
UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale); UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale);
[image drawAtPoint:CGPointMake(0, 0)]; [image drawAtPoint:CGPointZero];
if (_annotations.count > 0 ) {
// Get a standard annotation view pin. Future implementations should use a custom annotation image property. // Get a standard annotation view pin. Future implementations should use a custom annotation image property.
MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:@""]; MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:@""];
UIImage *pinImage = pin.image; UIImage *pinImage = pin.image;
for (id<MKAnnotation>annotation in _annotations) CGSize pinSize = pin.bounds.size;
{
for (id<MKAnnotation> annotation in _annotations) {
CGPoint point = [snapshot pointForCoordinate:annotation.coordinate]; CGPoint point = [snapshot pointForCoordinate:annotation.coordinate];
if (CGRectContainsPoint(finalImageRect, point)) if (CGRectContainsPoint(finalImageRect, point)) {
{
CGPoint pinCenterOffset = pin.centerOffset; CGPoint pinCenterOffset = pin.centerOffset;
point.x -= pin.bounds.size.width / 2.0; point.x -= pinSize.width / 2.0;
point.y -= pin.bounds.size.height / 2.0; point.y -= pinSize.height / 2.0;
point.x += pinCenterOffset.x; point.x += pinCenterOffset.x;
point.y += pinCenterOffset.y; point.y += pinCenterOffset.y;
[pinImage drawAtPoint:point]; [pinImage drawAtPoint:point];
} }
} }
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
} }
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext(); self.image = image;
UIGraphicsEndImageContext();
self.image = finalImage;
} }
}]; }];
} }