ios - MKMarkerAnnotationView 在 iPhone X 上不显示

标签 ios swift iphone mapkit

在较旧的手机(iPhone 7、8)上,为 MKMapSnapshotter 创建的 MKMarkerAnnotationView 可以在 map 上正确显示。仅在 iPhone X 及更高版本上它们不显示。我不知道可能是什么问题。这是创建快照的代码

static func generateMap(coordinates: [CLLocationCoordinate2D], id: String){

  var annotationViews = [MKMarkerAnnotationView]()
  let startCoord = coordinates.first!
  let endCoord = coordinates.last!

  ///create mapmarker
  func createMapMarkerAnnotation(text: String, coordinates: CLLocationCoordinate2D, color: UIColor) -> MKMarkerAnnotationView{
        let annotation = MKPointAnnotation()
        annotation.coordinate = coordinates

        let annotationView = MKMarkerAnnotationView(annotation: annotation, reuseIdentifier: "marker")
        annotationView.glyphText = text
        annotationView.glyphTintColor = .white
        annotationView.markerTintColor = color
        annotationView.contentMode = .scaleAspectFit
        annotationView.bounds = CGRect(x: 0, y: 0, width: 40, height: 40)
        return annotationView
    }

   //CREATE MARKERS
   annotationViews.append(createMapMarkerAnnotation(text: "A", coordinates: startCoord, color: Constants.primaryGreen))
   annotationViews.append(createMapMarkerAnnotation(text: "B", coordinates: endCoord, color: Constants.primaryGreen))

  let snapshotter = MKMapSnapshotter(options: options)
    snapshotter.start { (snapshot: MKMapSnapshotter.Snapshot?, error: Error?) -> Void in
        guard error == nil, let snapshot = snapshot else { return }

        let image = snapshot.image
        UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale);
        image.draw(at: CGPoint.zero) //image into context


        /// get the context for CoreGraphics
        let context = UIGraphicsGetCurrentContext()

        ///draw markers
        for view in annotationViews {
            let point: CGPoint = snapshot.point(for: view.annotation!.coordinate)
            view.drawHierarchy(in: CGRect(x: point.x - view.bounds.size.width / 2, y: point.y - view.bounds.size.height, width: view.bounds.width, height: view.bounds.height), afterScreenUpdates: true)
        }

        let finalImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()

        if let img = finalImage {
            let data: Data? = img.pngData()
            let content = data?.base64EncodedString()

            //write to db
            _ = DbTrips.shareInstance.updateTripMap(id: id, map: content!)
        }
    }
  }
}

有人知道是什么原因导致它们无法出现在较新的手机上吗?

谢谢^.^

最佳答案

因此,解决方案是在生成 map 和 View 时,需要在应用程序处于事件状态时进行。对于新手机,尝试在后台生成 View 是行不通的。在旧手机上会。为什么?我不知道。因此,需要发生的是,仅当应用程序处于事件状态时才需要生成 map 。这样做可以在所有设备上实现一致的生成。

我未能再次找到此问题在 SDK 文档中发布的位置,但我在那里找到了解决方案。

关于ios - MKMarkerAnnotationView 在 iPhone X 上不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58613580/

相关文章:

swift - 这段 Swift 代码中发生了什么

ios - 直接从 xib 文件实例化 UIViewController 哪个更好?

iphone - UINavigationBar 和 UIScrollView 调整大小

objective-c - 使用 CGImageMaskCreate 创建蒙版是全黑的(iphone)

ios - 如何使用项目的价格对 UICollectionView 项目进行排序/排序?

ios - 如何一次将多个图像传递给其他 View ?

swift - 在 swift 4 中使用 SocketIO 时丢失对变量的引用

ios - 如何在 iPhoneSimulator 中识别 iOS 8 和 iOS 9 模拟器?

ios - 如何在深色模式下单击按钮时使状态栏文本颜色变暗?

iphone - Siri SDK 可用吗?