我有一个UITableView,每个单元格都应该显示一个地图。由于在一个视图中有几个独立的skmappview实例是已知的限制(请参见http://developer.skobbler.com/getting-started/ios#sec29),而且我不希望在每个单元格中都有skmappview(性能),所以我想我应该使用renderMapImageInBoundingBox并创建要在表单元格中显示的映射图像。
我已经尝试了几件事,并删除了代码,只为第一个地图渲染图像。这是我在UITableViewController的viewDidAppear中所拥有内容的精简版本(我也尝试了viewDidLoad,但什么都没有)。
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(true)
//Initialize, set delegate, and customize map appearance
var mapView = SKMapView(frame: CGRectMake(0, 0, 963, 200))
mapView.delegate = self
mapView.applySettingsFromFileAtPath(JSONFilePath)
mapView.mapScaleView.hidden = true
mapView.settings.showAccuracyCircle = false
mapView.settings.panningEnabled = false
mapView.settings.rotationEnabled = false
mapView.settings.showStreetBadges = false
mapView.settings.showHouseNumbers = false
mapView.settings.showMapPoiIcons = false
mapView.settings.showOneWays = false
//Draw a track on the map
let track = tracks[0]
var polyline = SKPolyline()
polyline.coordinates = track.points
polyline.fillColor = themeTintColor
polyline.lineWidth = 3
polyline.backgroundLineWidth = 4
polyline.borderDotsSize = 1
polyline.borderDotsSpacingSize = 5
mapView.addPolyline(polyline)
//Set BoundingBox
let boundingBox = SKBoundingBox(topLeftCoordinate: CLLocationCoordinate2DMake(track.ne.coordinate.latitude, track.sw.coordinate.longitude), bottomRightCoordinate: CLLocationCoordinate2DMake(track.sw.coordinate.latitude, track.ne.coordinate.longitude))
// Add the mapView to the view just to see if it displays correctly. (It does!)
mapView.fitBounds(boundingBox, withPadding: CGSizeMake(20, 20))
self.view.addSubview(mapView)
// Actual PNG rendering
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
mapView.renderMapImageInBoundingBox(boundingBox, toPath: documentsPath, withSize: CGSizeMake(1000, 220))
println("Rendering in progress")
}
func mapViewDidFinishRenderingImageInBoundingBox(mapView: SKMapView!) {
println("ImageRenderingFinished")
}
mapView显示正确(边界框正确),在日志中,我看到“渲染正在进行”消息,但未调用mapViewDidFinishRenderingImageInBoundingBox,也未看到在我的应用程序容器中创建的png文件。没有编译器或运行时错误。(顺便说一下,我没有忘记添加SKMapViewDelegate)
有人知道我做错了什么吗?还是我碰到了虫子?
使用Xcode 6.1,在Swift中为iOS8开发。
最佳答案
很抱歉。我犯了一个非常愚蠢的错误。
我把documentsPath作为函数的参数,它是一个目录,但是函数需要一个完整的路径,包括文件名。
这使它发挥了作用:
let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
let imagePath = documentsPath.stringByAppendingPathComponent("images/mapImage.png")
mapView.renderMapImageInBoundingBox(boundingBox, toPath: imagePath, withSize: CGSizeMake(1000, 220))
我应该多喝点咖啡!
关于ios - renderMapImageInBoundingBox在iOS上不执行任何操作,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28439102/