我有一个UITableView,其中每个单元格应显示一个地图。因为在一个视图中有几个独立的SKMapView实例是已知的限制(参见http://developer.skobbler.com/getting-started/ios#sec29),我不想在每个单元格中都想要一个SKMapView(性能)我认为我是使用renderMapImageInBoundingBox并创建要在tableCells中显示的地图图像。
我已经尝试了几件事情,并删除了代码,只为第一张地图渲染图片。这是我在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,我没有看到在我的App容器中创建的png文件。没有编译器或运行时错误。 (顺便说一下,我没有忘记添加SKMapViewDelegate)
任何人都知道我做错了什么?或者我遇到了一个错误? 使用Xcode 6.1,在Swift中为iOS8开发。
答案 0 :(得分:0)
对此感到抱歉。我做了一个非常非常愚蠢的错误。 我将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))
我应该多喝咖啡!