我正在尝试使用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();
Run Code Online (Sandbox Code Playgroud)
现在我有了图像,我不知道如何将快照实际显示到我的mapview.
任何帮助将不胜感激!
因此 MKMapSnapshotter 类不用于在地图视图内部显示。它仅返回与地图视图不兼容的地图图像。当我需要额外的性能时,例如在表视图单元格或集合视图中显示带有图钉的地图时,我会使用它。它的性能比在表格视图内显示实际的地图视图要好得多。
因此,一旦获得快照,您所做的就是将其加载到 UIImageView 中。它看起来就像地图视图一样。
| 归档时间: |
|
| 查看次数: |
582 次 |
| 最近记录: |