我认为iOS 7的MKMapSnapshotter是拍摄MKMapView快照的一种简单方法,好处是您可以在不将地图加载到视图的情况下这样做。尽管添加引脚和图层似乎需要更多的工作(因为需要核心图形)。WWDC视频提供了一个很好的例子,通过添加一个MKMapSnapshotter创建了一个MKAnnotationView。
然而,对于没有太多核心图形经验的人来说,从MKMapSnapshotter创建MKPolylineRenderer并不是很明显。
我曾试过这样做,但这条路是不准确的。它精确地绘制了大约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();
}
}];请问有没有人有一个好的例子来说明如何做到这一点?
发布于 2014-03-28 15:12:34
只是遇到了同样的问题,这段代码似乎奏效了:
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();https://stackoverflow.com/questions/22692449
复制相似问题