gpt4 book ai didi

ios - MKMapSnapshotOptions : Adding snapshot of Custom Pin Annotation View or UIView

转载 作者:搜寻专家 更新时间:2023-10-30 21:52:50 27 4
gpt4 key购买 nike

我正在尝试使用 MKMapSnapshotter 的 startWithCompletionHandler 方法获取 map View 的快照。我想将自定义 Pin 注释 View 添加到快照中。并且在我的自定义注释 View 中有一个标签。所以我在获取快照时无法显示该标签。这是代码:

 let snapshotter = MKMapSnapshotter(options: options)
snapshotter.startWithCompletionHandler() {
snapshot, error in

if error != nil {
completion(image: nil, error: error)
return
}

let image = snapshot.image
let pin = MKPinAnnotationView(annotation: nil, reuseIdentifier: "") // I want to use custom annotation view instead of MKPinAnnotationView
let pinImage = UIImage(named: "pinImage")

UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale);
image.drawAtPoint(CGPointMake(0, 0))
var homePoint = snapshot.pointForCoordinate(coordinates[0])
pinImage!.drawAtPoint(homePoint)

pinImage!.drawAtPoint(snapshot.pointForCoordinate(coordinates[1]))

let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
completion(image: finalImage, error: nil)
}

如您所见,drawAtPoint 是 UIImage 的函数。我尝试使用 UIImageView 然后我将标签添加到 imageView 作为 subView 但我不能将 drawAtPoint 与 imageView 一起使用所以我的问题是我无法将标签添加到 mapView 快照。

你可以在链接中明白我的意思: https://www.dropbox.com/s/83hnkiqi87uy5ab/map.png?dl=0

感谢您的建议。

最佳答案

创建自定义 AnnotationView 类。当您创建 MKMapSnapshotter 时,使用坐标和标题定义 MKPointAnnotation。之后从您的自定义类定义 annotationView。您可以使用 MKPointAnnotation 初始化自定义 annotationView。并使用 drawViewHierarchyInRect 方法代替 drawAtPoint。

你的代码必须是这样的。

    let snapshotter = MKMapSnapshotter(options: options)
snapshotter.startWithCompletionHandler() {
snapshot, error in

if error != nil {
completion(image: nil, error: error)
return
}

let image = snapshot.image
var annotation = MKPointAnnotation()
annotation.coordinate = coordinates[0]
annotation.title = "Your Title"

let annotationView = CustomAnnotationView(annotation: annotation, reuseIdentifier: "annotation")
let pinImage = annotationView.image

UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale);

image.drawAtPoint(CGPointMake(0, 0)) //map

// pinImage!.drawAtPoint(snapshot.pointForCoordinate(coordinates[0]))

annotationView.drawViewHierarchyInRect(CGRectMake(snapshot.pointForCoordinate(coordinates[0]).x, snapshot.pointForCoordinate(coordinates[0]).y, annotationView.frame.size.width, annotationView.frame.size.height), afterScreenUpdates: true)


let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
completion(image: finalImage, error: nil)
}

关于ios - MKMapSnapshotOptions : Adding snapshot of Custom Pin Annotation View or UIView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32121778/

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