ios - 如何在 MKMapSnapshotter 上绘制 CLLocationCooperative2Ds(在 mapView 打印图像上绘制)

标签 ios swift mapkit mkmapsnapshotter

我有带有CLLocationCooperative2D数组的mapView。我使用 MKPolyline 使用这些位置在我的 map View 上绘制线条。现在我想将其存储为 UIimage。我发现有类 MKMapSnapshotter 但不幸的是我无法在其上绘制叠加层“Snapshotter 对象不会捕获您的应用程序创建的任何叠加层或注释的视觉表示。”所以我只得到空白 map 图像。有什么方法可以用我的叠加层获取图像吗?

private func generateImageFromMap() {
    let mapSnapshotterOptions = MKMapSnapshotter.Options()
    guard let region = mapRegion() else { return }
    mapSnapshotterOptions.region = region
    mapSnapshotterOptions.size = CGSize(width: 200, height: 200)
    mapSnapshotterOptions.showsBuildings = false
    mapSnapshotterOptions.showsPointsOfInterest = false


    let snapShotter = MKMapSnapshotter(options: mapSnapshotterOptions)
    snapShotter.start() { snapshot, error in
        guard let snapshot = snapshot else {
//do something with image .... 
            let mapImage = snapshot...
        }
    }
}

如何在此图像上添加叠加层?或者也许还有其他方法可以解决这个问题。

最佳答案

不幸的是,你必须自己画它们。幸运的是,MKSnapshot 有一个方便的 point(for:)方法将 CLLocationCooperative2D 转换为快照内的 CGPoint

例如,假设您有一个 CLLocationCooperative2D 数组:

private var coordinates: [CLLocationCoordinate2D]?

private func generateImageFromMap() {
    guard let region = mapRegion() else { return }

    let options = MKMapSnapshotter.Options()
    options.region = region
    options.size = CGSize(width: 200, height: 200)
    options.showsBuildings = false
    options.showsPointsOfInterest = false

    MKMapSnapshotter(options: options).start() { snapshot, error in
        guard let snapshot = snapshot else { return }

        let mapImage = snapshot.image

        let finalImage = UIGraphicsImageRenderer(size: mapImage.size).image { _ in

            // draw the map image

            mapImage.draw(at: .zero)

            // only bother with the following if we have a path with two or more coordinates

            guard let coordinates = self.coordinates, coordinates.count > 1 else { return }

            // convert the `[CLLocationCoordinate2D]` into a `[CGPoint]`

            let points = coordinates.map { coordinate in
                snapshot.point(for: coordinate)
            }

            // build a bezier path using that `[CGPoint]`

            let path = UIBezierPath()
            path.move(to: points[0])

            for point in points.dropFirst() {
                path.addLine(to: point)
            }

            // stroke it

            path.lineWidth = 1
            UIColor.blue.setStroke()
            path.stroke()
        }

        // do something with finalImage
    }
}

然后是以下 map View (坐标为 MKPolyline,由 mapView(_:rendererFor:) 渲染,像往常一样):

enter image description here

上面的代码将创建这个finalImage:

enter image description here

关于ios - 如何在 MKMapSnapshotter 上绘制 CLLocationCooperative2Ds(在 mapView 打印图像上绘制),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54678314/

相关文章:

ios - "[VKDefault] Tile in current unloaded state"iOS 11 map 不工作

ios - 向 uiview 添加从 A 点移动到 B 点的动画

ios - 自动递增到不同节表中的每个单元格

android - 如何在 android 或 ios 移动设备上运行 Nodejs 运行时

html - 如何正确地将 HTML 文件加载到 UIWebView 中,而不会因为 nil 值而崩溃? ( swift )

ios - 无法将类型 '__NSCFArray' 的值转换为 'NSDictionary' + 在 map View 中显示注释

ios - 在 Swift 中使用动画调整 CALayer 的大小

json - 无法获取路径 Swift Playground 错误的沙箱扩展

ios - MKMapView 缩放以显示框架中的所有注释

iphone - 通过标签栏访问 MKMapView