如何在 MKMapSnapshotter 上绘制 CLLocationCoordinate2Ds(在 mapView 打印图像上绘制)
How to draw a CLLocationCoordinate2Ds on MKMapSnapshotter (drawing on mapView printed image)
我的 mapView 数组为 CLLocationCoordinate2D
。我使用这些位置通过 MKPolyline
在我的 mapView 上画线。现在我想将它存储为 UIimage。我发现有 class MKMapSnapshotter
但不幸的是我无法在其上绘制叠加层 "Snapshotter objects do not capture the visual representations of any overlays or annotations that your app creates." 所以我只得到空白地图图像。有什么方法可以用我的叠加层获取图像吗?
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:)
方法可以在快照中将 CLLocationCoordinate2D
转换为 CGPoint
。
例如,假设您有一个数组 CLLocationCoordinate2D
:
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
}
}
然后是以下地图视图(坐标为 MKPolyline
,由 mapView(_:rendererFor:)
渲染,像往常一样):
以上代码将创建这个 finalImage
:
我的 mapView 数组为 CLLocationCoordinate2D
。我使用这些位置通过 MKPolyline
在我的 mapView 上画线。现在我想将它存储为 UIimage。我发现有 class MKMapSnapshotter
但不幸的是我无法在其上绘制叠加层 "Snapshotter objects do not capture the visual representations of any overlays or annotations that your app creates." 所以我只得到空白地图图像。有什么方法可以用我的叠加层获取图像吗?
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:)
方法可以在快照中将 CLLocationCoordinate2D
转换为 CGPoint
。
例如,假设您有一个数组 CLLocationCoordinate2D
:
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
}
}
然后是以下地图视图(坐标为 MKPolyline
,由 mapView(_:rendererFor:)
渲染,像往常一样):
以上代码将创建这个 finalImage
: