我原以为iOS 7的MKMapSnapshotter
可以简单地对MKMapView
进行截图,这样做的好处是不需要将地图加载到视图中。即使添加标记和覆盖物似乎更加麻烦(因为需要核心图形库),WWDC视频也很好地演示了如何在MKMapSnapshotter
中创建一个MKAnnotationView
。
然而,对于没有太多核心图形库经验的人来说,如何从MKPolylineRenderer
创建MKMapSnapshotter
并不是很明显。
我已经尝试过这样做,但路径不准确。它只能准确地绘制线路的约10%,然后就直接绘制剩余的路径了。
这是我的代码:
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:snapshotOptions];
[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
completionHandler:^(MKMapSnapshot *snapshot, NSError *error)
{
if (error == nil)
{
UIImage *mapSnapshot = [snapshot image];
UIGraphicsBeginImageContextWithOptions(mapSnapshot.size,
YES,
mapSnapshot.scale);
[mapSnapshot drawAtPoint:CGPointMake(0.0f, 0.0f)];
CGContextRef context = UIGraphicsGetCurrentContext();
//Draw the points from the MKPolylineRenderer in core graphics for mapsnapshotter...
MKPolylineRenderer *overlay = (MKPolylineRenderer *)[self.mapView rendererForOverlay:[_mapView.overlays lastObject]];
if (overlay.path)
{
CGFloat zoomScale = 3.0;
[overlay applyStrokePropertiesToContext:context
atZoomScale:zoomScale];
CGContextAddPath(context, overlay.path);
CGContextSetLineJoin(context, kCGLineJoinRound);
CGContextSetLineCap(context, kCGLineCapRound);
CGContextStrokePath(context);
}
UIImage *pathImage = UIGraphicsGetImageFromCurrentImageContext();
[map addMapIcon:pathImage];
UIGraphicsEndImageContext();
}
}];
有没有人能给个可行的例子来说明如何做到这一点?