gpt4 book ai didi

ios - 使用 MKPolylineRenderer 创建 MKMapSnapshotter

转载 作者:技术小花猫 更新时间:2023-10-29 10:36:00 33 4
gpt4 key购买 nike

我认为 iOS 7 的 MKMapSnapshotter 将是拍摄 MKMapView 快照的简单方法,好处是您可以在不将 map 加载到 View 中的情况下执行此操作.尽管添加图钉和叠加层似乎需要更多工作(因为需要核心图形)。 WWDC 视频提供了一个很好的示例,说明如何通过添加 MKAnnotationView 创建 MKMapSnapshotter

但是,对于核心图形经验不多的人来说,如何从 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();
}
}];

有没有人有一个很好的可行示例来说明如何执行此操作?

最佳答案

刚遇到同样的问题,这段代码似乎有效:

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

关于ios - 使用 MKPolylineRenderer 创建 MKMapSnapshotter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22692449/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com