5
我正在嘗試使用MKSnapshotter在我的應用中固定位置的街景視圖的快照。目前,我有這樣的代碼:如何在Swift中顯示MKSnapshot(iOS)
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();
現在我的形象,我不知道如何實際顯示快照我的MapView。
任何幫助將不勝感激!