MKMapSnapshotOptions:添加Custom Pin Annotation View或UIView的快照 [英] MKMapSnapshotOptions : Adding snapshot of Custom Pin Annotation View or UIView

查看:151
本文介绍了MKMapSnapshotOptions:添加Custom Pin Annotation View或UIView的快照的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用MKMapSnapshotter的startWithCompletionHandler方法获取地图视图的快照。我想将自定义图钉注释视图添加到快照。并且我的自定义注释视图中有一个标签。所以当我正在拍摄快照时,我无法显示该标签。
这里是代码:

I am trying to get snapshot of map view with startWithCompletionHandler methods of MKMapSnapshotter. and I want to add Custom Pin Annotation View to snap shot. and there is a label in my custom annotation view. so I can not show that label when ı am getting snapshot. here is the code:

 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快照添加标签。

as you can see drawAtPoint is function of UIImage. I try to use UIImageView then I add label to imageView as subView but I cannot use drawAtPoint with imageView so my problem is I cannot add label to mapView snapshot.

你可以看到我的意思是链接:
https://www.dropbox .com / s / 83hnkiqi87uy5ab / map.png?dl = 0

you can see what I mean at link : https://www.dropbox.com/s/83hnkiqi87uy5ab/map.png?dl=0

感谢您的建议。

推荐答案

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

Create Custom AnnotationView Class.When you create MKMapSnapshotter define MKPointAnnotation with coordinate and title.After that define annotationView from your Custom Class.You can init custom annotationView with MKPointAnnotation. And use drawViewHierarchyInRect method instead of drawAtPoint.

您的代码必须是这样的。

Your code must be like that.

    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)
    }

这篇关于MKMapSnapshotOptions:添加Custom Pin Annotation View或UIView的快照的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆