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

标签 ios objective-c swift mkmapsnapshotter

我正在尝试使用 MKMapSnapshotter 的 startWithCompletionHandler 方法获取 map View 的快照。我想将自定义引脚注释 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/59993671/

相关文章:

php - Alamofire 不发布到 PHP 脚本

iphone - 重新测试应用内购买

swift - CIDepthBlurEffect : setting inputFocusRect

objective-c - 从 Objective-C 到 Swift 4 的一行代码

iphone - UIViewController 中的继承

ios - 如何使用省略号自动截断单词边界上的 UILabel?

objective-c - 错误 - 架构 arm64 的 2 个重复符号?

ios - 使用 XCode (Swift) 为学校和标签属性开发 iOS 应用程序不会刷新

ios - 从 Mac 读取 iPhone 日志文件内容

ios - 缩放后文字变得模糊