首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MKMapSnapshotter创建MKPolylineRenderer

使用MKMapSnapshotter创建MKPolylineRenderer
EN

Stack Overflow用户
提问于 2014-03-27 15:39:26
回答 1查看 3.6K关注 0票数 13

我认为iOS 7的MKMapSnapshotter是拍摄MKMapView快照的一种简单方法,好处是您可以在不将地图加载到视图的情况下这样做。尽管添加引脚和图层似乎需要更多的工作(因为需要核心图形)。WWDC视频提供了一个很好的例子,通过添加一个MKMapSnapshotter创建了一个MKAnnotationView

然而,对于没有太多核心图形经验的人来说,从MKMapSnapshotter创建MKPolylineRenderer并不是很明显。

我曾试过这样做,但这条路是不准确的。它精确地绘制了大约10%的直线,但随后将其余的路径绘制为直线。

这是我的密码:

代码语言:javascript
复制
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();
            }
}];

请问有没有人有一个好的例子来说明如何做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-28 15:12:34

只是遇到了同样的问题,这段代码似乎奏效了:

代码语言:javascript
复制
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();
票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22692449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档