[英]MKMarkerAnnotationView don't show on iPhone X
在旧手机(iPhone 7、8)上,为MKMarkerAnnotationView
创建的MKMapSnapshotter
在 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 和视图时,它需要在应用程序处于活动状态时发生。 对于新手机,尝试在后台生成视图是行不通的。 在旧手机上会。 为什么? 我不知道。 因此,需要发生的是 map 生成只需要在应用程序处于活动状态时发生。 这样做会在所有设备上产生一致的生成。
我一直未能再次找到 SDK 文档中发布的位置,但这就是我找到解决方案的地方。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.