如何在Swift(iOS)中显示MKSnapshot

5

我正在尝试使用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;
        }
        let 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 as! [MKAnnotation]{
            var point:CGPoint = snapshot.pointForCoordinate(annotation.coordinate);
            if(CGRectContainsPoint(rect, point)) {
                point.x = point.x + pin.centerOffset.x - (pin.bounds.size.width / 2.0);
                point.y = point.y + pin.centerOffset.y - (pin.bounds.size.height / 2.0);
                pin.image.drawAtPoint(point);
            }
        }

        var compositeImage:UIImage = UIGraphicsGetImageFromCurrentImageContext();
        var data2:NSData = UIImagePNGRepresentation(compositeImage);
        data.writeToURL(fileURL, atomically: true);
        UIGraphicsEndImageContext();

现在我已经拥有这个图片,但我不知道如何将快照实际显示到我的地图视图中。

非常感谢您的帮助!

1个回答

0

因此,MKMapSnapshotter类不用于在地图视图中显示。它只返回一张与地图视图不兼容的地图图片。当我需要额外的性能,例如在表格视图单元格或集合视图中显示带有标记的地图时,我会使用它。它比在表格视图中显示实际的地图视图要好得多。

因此,一旦您拍摄了快照,您所要做的就是将其加载到UIImageView中。它看起来就像地图视图一样。


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