- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试在 MKMapSnapshotter
中绘制一个 MKCircleRenderer
。
所以我需要的是:
所以我有:
我的代码:
+ (void)renderMapRegion:(MKCoordinateRegion)region
withSize:(CGSize)size
annotation:(MKAnnotationView *)annotation
circleRenderer:(MKCircleRenderer *)circleRenderer
completion:(CMMapViewRendererCallback)block
{
MKMapSnapshotOptions *options = [MKMapSnapshotOptions new];
options.region = region;
options.scale = [UIScreen mainScreen].scale;
options.size = size;
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if (error) {
block(nil, error);
return;
}
UIImage *image = snapshot.image;
UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale);
{
[image drawAtPoint:CGPointMake(0.0f, 0.0f)];
CGContextRef c = UIGraphicsGetCurrentContext();
if (circleRenderer.path) {
[circleRenderer applyFillPropertiesToContext:c atZoomScale:[UIScreen mainScreen].scale];
CGContextAddPath(c, circleRenderer.path);
CGContextFillPath(c);
}
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
CGPoint point = [snapshot pointForCoordinate:annotation.annotation.coordinate];
if (CGRectContainsPoint(rect, point)) {
point.x = point.x + annotation.centerOffset.x - (annotation.bounds.size.width / 2.0f);
point.y = point.y + annotation.centerOffset.y - (annotation.bounds.size.height / 2.0f);
[annotation.image drawAtPoint:point];
}
UIImage *compositeImage = UIGraphicsGetImageFromCurrentImageContext();
block(compositeImage,nil);
}
UIGraphicsEndImageContext();
}];
}
我的注释是正确的,但是 MKCircleRenderer
绘制在错误的坐标中。
更新 1
经过一番尝试。我只有一个问题:正确的半径大小。我的圆圈有 15 米的半径值。但是当我在 map 上打印时有一个 119(我不知道这个单位)
private func renderMap(#placemark: CLPlacemark?, annotationView: MKAnnotationView) {
self.tempPlaceMark = placemark
let region = MKCoordinateRegionMakeWithDistance(annotationView.annotation.coordinate, 5000, 5000)
let circle = MKCircle(centerCoordinate: annotationView.annotation.coordinate, radius: radius!)
var render = MKCircleRenderer(circle: circle)
render.fillColor = Color.YellowColor
CMMapViewRenderer.renderMapRegion(region, withSize: self.mapView.frame.size, annotation: annotationView, circleRenderer: render) { (image: UIImage!, error: NSError!) -> Void in
dispatch_async(dispatch_get_main_queue()) {
if error == nil {
self.tempCoordinate = annotationView.annotation.coordinate
self.tempSnapshotImage = image
}
self.dismissProgress()
}
}
}
基于此解决方案:https://stackoverflow.com/a/8288467/846780
CGPoint circlePoint = [snapshot pointForCoordinate:annotation.annotation.coordinate];
CGRect boundingRect = [circleRenderer rectForMapRect:circleRenderer.circle.boundingMapRect];
CGFloat radius = boundingRect.size.width / 2;
CGContextSetStrokeColorWithColor(c, circleRenderer.fillColor.CGColor);
CGContextSetFillColorWithColor(c, circleRenderer.fillColor.CGColor);
CGContextSetLineWidth(c, 1);
CGContextSetShouldAntialias(c, YES);
CGContextAddArc(c, circlePoint.x, circlePoint.y, radius, 0, 2 * M_PI, true);
CGContextDrawPath(c, kCGPathFill);
最佳答案
另一种方法是自己绘制此 MKCircle
,将 center
和 radius
属性转换为 View 中的坐标。这会在当前上下文中呈现圆圈(我已将其用于将 MKCircle
叠加层添加到快照图像):
// use whatever colors you want
[[UIColor blueColor] setStroke];
[[[UIColor cyanColor] colorWithAlphaComponent:0.7] setFill];
// in my code, I'm iterating through the `overlays` objects, so I test to
// see if it's a circle and cast appropriately
if ([overlay isKindOfClass:[MKCircle class]]) {
CLLocationCoordinate2D coordinate = [(MKCircle *)overlay coordinate];
CGPoint center = [snapshot pointForCoordinate:coordinate];
CLLocationDistance radiusInMeters = [(MKCircle *)overlay radius];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coordinate, radiusInMeters, 0);
CGRect rect = [self.mapView convertRegion:region toRectToView:self.mapView];
CGFloat radiusInPoints = rect.size.height;
UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:center radius:radiusInPoints startAngle:0 endAngle:M_PI * 2.0 clockwise:TRUE];
if (CGRectIntersectsRect([path bounds], CGRectMake(0, 0, image.size.width, image.size.height))) {
[path stroke];
[path fill];
}
}
关于ios - 在 MKMapSnapshotter 上绘制一个 MKCircleRenderer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30468060/
我正在使用 MKMapSnapshotter 创建 MKMapView 的快照。这非常有效: let centerCoordinate = CLLocationCoordinate2DMake(-33
我有一个应用程序应该在后台获取对象并使用它们的位置数据为它们生成 map 快照。我自然而然地尝试了 MKMapSnapshotter。 事实证明(在对黑色 map 快照感到困惑数周之后)这个工具似乎只
我的 awakeWithContext 中有一个正在运行的 MKMapSnapshotter,我想为我的 imageView 设置它的图像。 问题是,MKMapSnapshotter 速度太慢,无法设
我正在使用 MKMapSnapshotter 创建 map 的屏幕截图,然后将其显示在 UIImageView 中。 let imageView = UIImageView() imageView.f
在连接到 Apple Watch 的真实 iPhone 上测试我的“ watch 应用程序”/iOS 应用程序 watch 扩展时,我遇到了 MKSnapshotter 的一些重大问题! 在我的一个接
我创建图钉图像的代码如下: MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init]; options.region =
我使用以下函数在 MKMapView 的 UIImage 快照上绘制折线。该功能有效,但对我来说太慢了。有什么办法可以加快速度吗? func drawLineOnImageFromLocations(
是否有任何原因导致 MKMapSnapshotter 的 startWithCompletionHandler 方法上的完成处理程序不会被回调? (甚至没有错误!!) 我没有做任何花哨的事情,只是抓取
我正在使用 MKMapSnapshotter 创建一个小型 MKMapView 的 UIImage 屏幕截图(并存储以备后用)。但我注意到的一件事是它从快照中删除了“合法”标签。 Here答案是删除“
我们有一个 iOS 应用程序,它有一个 UITableView,其中包含在单元格出现时生成的 map 快照。我们使用的示例列表只是显示基于模型类中提供的纬度/经度的 map 快照。我开始注意到内存崩溃
我对 MKMapSnapshotter 有疑问。我目前有两个 View ,一个带有 UIButton,显示 UIActivityViewController,另一个带有 MKMapView,显示当前用
自 iOS 13 起,此代码返回黑色图像。任何想法? 我还记录了这些警告: InfoLog FlyoverNightShader: WARNING: 0:29: Overflow in implici
我正在尝试在 MKMapSnapshotter 中绘制一个 MKCircleRenderer。 所以我需要的是: 所以我有: 我的代码: + (void)renderMapRegion:(MKCoor
我正在尝试使用新的 iOS7 MKMapSnapshotter 生成静态 map 图像。每当我的应用程序需要 map 时,我都会调用以下命令: MKMapSnapshotter *snapshotte
我认为 iOS 7 的 MKMapSnapshotter 将是拍摄 MKMapView 快照的简单方法,好处是您可以在不将 map 加载到 View 中的情况下执行此操作.尽管添加图钉和叠加层似乎需要
我正在自己学习 Swift 3,我当前的学习项目涉及允许用户拍摄照片并获取固定当前位置的 map 快照。 我依赖 this answer从 2015 年 8 月和 this answer从 2016
我有一个 UITableView,它可能包含大量单元格,在某些单元格中,我想要一个 UIImageView 来显示一个位置的 map ,其中的坐标仅在运行时已知。 我希望表格 View 能够平滑滚动,
我想在 UITableViewCells 中显示 mapView, 所以我实际上写了这段代码 class DataPointTableCell: UITableViewCell { @IBOu
我有一个奇怪的问题:我像往常一样使用 openParentApplication:reply: 调用父应用程序。 使用异步 NSURLRequests 从 Internet 获取一些数据的工作做得很好
我正在开发适用于 MKMapSnapshotter API 的应用。 它制作具有所需区域的 map 快照。 它的代码从前台应用程序状态和主线程调用,如下所示: MKMapSnapshotOptions
我是一名优秀的程序员,十分优秀!