Swift 3 将自定义注释图钉添加到 MKMapSnapShotter 快照

Swift 3 Add custom annotation pin to MKMapSnapShotter snapshot

我正在自学 Swift 3,我目前的学习项目涉及允许用户拍摄照片并获得固定当前位置的地图快照。

我从 2016 年 6 月起一直依赖 from Aug 2015 and 进行指导,但我仍然找不到正确的路径。

现在,我可以...

  1. 从缓冲区中获取照片
  2. 获取地图快照

但我无法放置图钉。我知道我的代码不完整且无效——所以这不仅仅是一个调试问题。这是我一直在使用的(以及基于上述链接的许多变体):

let snapShotter = MKMapSnapshotter(options: mapSnapshotOptions)

snapShotter.start() {

    snapshot, error in

        guard let snapshot = snapshot else {
            return
        }

        let image = snapshot.image
        let annotation = MKPointAnnotation()
        annotation.coordinate = needleLocation  // is a CLLocationCoordinate2D
        annotation.title = "My Title"

        let annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "annotation")

        // I want to push the final image to a global variable
        // can't figure out how to define finalImage as the pinned map
        self.myMap = finalImage


        } // close snapShotter.start

我已经被困了好几天了,所以我当然会很感激任何见解。谢谢!

要渲染带有注释视图的 MKMapSnapshot,您必须在新的图形上下文中手动绘制快照的 image 和注释视图的 image,并从中获取新图像那个上下文。在 Swift 3:

let rect = imageView.bounds

let snapshot = MKMapSnapshotter(options: options)
snapshot.start { snapshot, error in
    guard let snapshot = snapshot, error == nil else {
        print(error ?? "Unknown error")
        return
    }

    let image = UIGraphicsImageRenderer(size: options.size).image { _ in
        snapshot.image.draw(at: .zero)

        let pinView = MKPinAnnotationView(annotation: nil, reuseIdentifier: nil)
        let pinImage = pinView.image

        var point = snapshot.point(for: location.coordinate)

        if rect.contains(point) {
            point.x -= pinView.bounds.width / 2
            point.y -= pinView.bounds.height / 2
            point.x += pinView.centerOffset.x
            point.y += pinView.centerOffset.y
            pinImage?.draw(at: point)
        }
    }

    // do whatever you want with this image, e.g.

    DispatchQueue.main.async {
        imageView.image = image
    }
}

改编自, which itself was adapted from WWDC 2013 video Putting MapKit in Perspective

这是一个带有 .satelliteFlyover:

的快照示例

我从定义 "rect" 的 Rob 代码中收到错误,因为我的 MKMapSnapshotter 图像是 UIImage。但是 .bounds 是 UIImageView 的 属性 而不是 UIImage。所以 rect 定义会抛出错误。

以下是我配置选项的方式:

        let mapSnapshotOptions = MKMapSnapshotOptions()

        // Set the region of the map that is rendered.
        let needleLocation = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region = MKCoordinateRegionMakeWithDistance(needleLocation, 1000, 1000)
        mapSnapshotOptions.region = region

        // Set the scale of the image. We'll just use the scale of the current device, which is 2x scale on Retina screens.
        mapSnapshotOptions.scale = UIScreen.main.scale

        // Set the size of the image output.
        mapSnapshotOptions.size = CGSize(width: 300, height: 300)

        // Show buildings and Points of Interest on the snapshot
        mapSnapshotOptions.showsBuildings = true
        mapSnapshotOptions.showsPointsOfInterest = false

那么有没有其他方法可以访问 .bounds 属性?还是我创建的快照不正确?