gpt4 book ai didi

swift3 - Swift 3 向 MKMapSnapShotter 快照添加自定义注释引脚

转载 作者:行者123 更新时间:2023-12-04 04:56:41 26 4
gpt4 key购买 nike

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

我依赖 this answer从 2015 年 8 月和 this answer从 2016 年 6 月开始寻求指导,但我仍然找不到正确的路径。

此刻,我可以...

  • 从缓冲区获取照片
  • 获取 map 快照

  • 但我就是放不下图钉。我知道我的代码不完整且无效——所以这不仅仅是一个调试问题。这是我一直在使用的内容(以及基于上述链接的许多变体):
    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使用注释 View ,您必须手动绘制快照的 image和注释 View 的 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
    }
    }

    这是改编自 https://stackoverflow.com/a/18776723/1271826 ,其本身改编自 WWDC 2013 视频 Putting MapKit in Perspective .

    这是一个带有 .satelliteFlyover 的快照示例:

    enter image description here

    关于swift3 - Swift 3 向 MKMapSnapShotter 快照添加自定义注释引脚,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42757185/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com