使用MKPolylineRenderer创build一个MKMapSnapshotter

我认为iOS 7的MKMapSnapshotter将是一个简单的方法来拍摄一个MKMapView的快照,好处是你可以做到这一点,而无需将地图加载到视图中。 即使看起来更多的工作来添加引脚和覆盖(因为需要核心graphics)。 WWDCvideo给出了创buildMKMapSnapshotter并添加MKAnnotationView一个很好的例子。

然而,对于没有太多核心graphics体验的人来说,如何从MKPolylineRenderer创build一个MKMapSnapshotter并不是很明显。

我试图做到这一点,但path是不准确的。 它准确地绘制了约10%的线,但是直接绘制了其余的path。

这是我的代码:

 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(); } }]; 

有没有人有一个很好的可行的例子,如何做到这一点?

刚刚有同样的问题,这段代码似乎工作:

 UIImage * res = nil; UIImage * image = snapshot.image; UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale); [image drawAtPoint:CGPointMake(0, 0)]; CGContextRef context = UIGraphicsGetCurrentContext(); CGContextSetStrokeColorWithColor(context, [COLOR_FLASHBLUE CGColor]); CGContextSetLineWidth(context,2.0f); CGContextBeginPath(context); CLLocationCoordinate2D coordinates[[polyline pointCount]]; [polyline getCoordinates:coordinates range:NSMakeRange(0, [polyline pointCount])]; for(int i=0;i<[polyline pointCount];i++) { CGPoint point = [snapshot pointForCoordinate:coordinates[i]]; if(i==0) { CGContextMoveToPoint(context,point.x, point.y); } else{ CGContextAddLineToPoint(context,point.x, point.y); } } CGContextStrokePath(context); res = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();