使用MKPolylineRenderer创建一个MKMapSnapshotter

13

我原以为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();
            }
}];

有没有人能给个可行的例子来说明如何做到这一点?

1个回答

13

我刚刚遇到了同样的问题,这段代码似乎可以解决:

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

有趣的是,你必须显式地移动并将该行添加到指针中,而不能使用CGContextAddPath。谢谢,我还没有尝试过,但会去看看 :) - PostCodeism
我不确定你必须这样做。我不知道缩放级别,所以不得不使用pointForCoordiinate方法。 - Saa
这对我也起作用了。我仍然很好奇为什么CGContextAddPath不起作用 >_< - PostCodeism
哦,我多调用了一个(at: CGPoint.zero)的绘制函数,当我移除了这个多余的调用后,我的图钉重新出现了。但是这个调用是干什么用的呢? - shim
@shim将快照图像绘制到上下文中。你可能在原始地图上过度绘制了你的引脚? - Saa
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接