使用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();
- 使用私有框架以编程方式发送iMessage
- 在iOS 7.0设备上调用remoteControlReceivedWithEvent但不在iOS 8.0上调用
- IOS:CS193p fall2013任务2:添加一个button让用户重新启动游戏
- 了解iOS中的随机数字Swift 2
- 如何dequeueReusableCellWithIdentifier:对待不同的indexPath?
- 当我打开我在Xcode 4中创build的Google Maps项目时,Xcode 5会警告我的架构设置
- 如何在UILabel中find一个字母的位置(x,y)
- UIStackView中的第一个基线和最后一个基线属性
- CCLOG不显示