如何在Swift(iOS)中使用MKSnapshotter

3

我目前正在尝试从MapKit实现MKSnapshotter。 我想使用苹果地图在地图视图(街景)中创建我正在查找的位置的快照。

这是我到目前为止写的代码:

    var options: MKMapSnapshotOptions = MKMapSnapshotOptions();
    options.region = self.attractionDetailMap.region;
    options.size = self.attractionDetailMap.frame.size;
    options.scale = UIScreen.mainScreen().scale;

    var fileURL:NSURL = NSURL(fileURLWithPath: "path/to/snapshot.png")!;
    var snapshotter:MKMapSnapshotter = MKMapSnapshotter();
    snapshotter.startWithCompletionHandler { (snapshot:MKMapSnapshot!, error:NSError!) -> Void in
        if(error != nil) {
            println("error: " + error.localizedDescription);
            return;
        }
        var image:UIImage = snapshot.image;
        var data:NSData = UIImagePNGRepresentation(image);
        data.writeToURL(fileURL, atomically: true);

        var pin:MKAnnotationView = MKAnnotationView();
        UIGraphicsBeginImageContextWithOptions(image.size, true, image.scale);
        image.drawAtPoint(CGPointMake(0.0, 0.0));
        var rect:CGRect = CGRectMake(0.0, 0.0, image.size.width, image.size.height);
        for annotation in self.attractionDetailMap.annotations {
            var point:CGPoint = snapshot.pointForCoordinate(annotation.coordinate);
        }
    }

当我试图指定一个点 (var point:CGPoint = snapshot.pointForCoordinate(annotation.coordinate);) 时,出现错误:“coordinate” 不可用:在iOS7及更早版本中已经停用的API在Swift中不可用。

基本上,由于在swift中使用MKSnapshotter的教程/指南并不是很多,如果我能够看到MKSNapshotter在swift中的标准结构,那将非常感激。我觉得我走在正确的道路上,但我卡在了最后一行代码上,这甚至根本不起作用。
谢谢!
1个回答

1
您需要将注释转换为正确的类型。
for annotation in self.attractionDetailMap.annotations as! [MKAnnotation] {
    let point = snapshot.pointForCoordinate(annotation.coordinate)
}

谢谢指出。那么您如何设置街景图像并在我的VC上显示它呢? - kebabTiger
一旦你有了一张图片,你就可以创建一个控制器,其中包含一个带有图像视图的视图,并将你的图片传递到那里,在UIImageView中显示它。 - Tomáš Linhart

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接