如何在Swift(iOS)中显示MKSnapshot
发布时间:2020-12-14 04:54:28 所属栏目:百科 来源:网络整理
导读:我正在尝试使用MKSnapshotter拍摄我的应用程序中固定位置的街景视图.我目前有这个代码: var options: MKMapSnapshotOptions = MKMapSnapshotOptions(); options.region = self.attractionDetailMap.region; options.size = self.attractionDetailMap.frame.
我正在尝试使用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(); 现在我有了图像,我不知道如何将快照实际显示到我的mapview. 任何帮助将不胜感激! 解决方法
因此MKMapSnapshotter类不用于在mapview内部显示.它仅返回与mapview不兼容的地图图像.当我需要额外的性能时,我已经使用过它,比如在tableview单元格或collectionview中显示带有pin的地图.它比在tableview中显示实际的mapview更好.
所以,一旦你拥有快照,你所做的就是将它加载到UIImageView中.它看起来就像mapview一样. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |