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