MKMapSnapshotOptions : Adding snapshot of Custom Pin Annotation View or UIView

丶灬走出姿态 提交于 2019-12-03 07:18:36

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)
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!