use*_*930 6 uitableview mapkit mkmapview mkmapviewdelegate mkmapsnapshotter
在我的ios swift应用程序中,我有UITableViewController动态添加的单元格.每个单元都有一个MKMapView嵌入式,我正在为不同坐标上的每个单元设置地图的中心.我这样做是通过调用这个方法:
func centerMapOnLocation(location: CLLocation, map: MKMapView, radius: CLLocationDistance) {
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
radius * 2.0, radius * 2.0)
map.setRegion(coordinateRegion, animated: true)
}
Run Code Online (Sandbox Code Playgroud)
里面cellForRowAtIndexPath:
override func tableView(tview: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tview.dequeueReusableCellWithIdentifier("cell") as! SingleCall
let user:SingleUser = self.items[indexPath.row] as! SingleUser
let regionRadius: CLLocationDistance = 150
let initialLocation = CLLocation(latitude: user.coordinate.latitude, longitude: user.coordinate.longitude)
centerMapOnLocation(initialLocation, map: cell.eventMap, radius: regionRadius)
cell.eventMap.zoomEnabled = false
cell.eventMap.scrollEnabled = false
cell.eventMap.userInteractionEnabled = false
}
Run Code Online (Sandbox Code Playgroud)
这很好,它可以工作,但我想有很多记录会有内存问题 - 即使现在只有几个单元格,当用户滚动表格时 - 每个地图立即加载,我只能想象它需要多少计算能力所以.
所以我的问题是 - 有没有办法将动态地图视图更改为某种可能的地图实际位置截图?当涉及到大量细胞时,它会更快地运作吗?
小智 2
对的,这是可能的。为此,您应该改用 UIImageView(mapImageView在下面的代码中调用)和 MKMapSnapshotter。为了让事情真正飞起来,您应该缓存 MKMapSnapshotter 生成的图像,以便当用户滚动回之前看到的单元格时立即加载它们。这种方法对资源很温和,因为它只使用一个全局 MKMapSnapShotter,而不是每个单元格的地图。
这是我根据您的情况进行调整的一些代码。我没有详细介绍如何具体缓存,因为这取决于您希望如何在应用程序中处理缓存。我过去使用过 Haneke cocoapod:https://cocoapods.org/pods/Haneke 。另外,我在下面设置了许多常见的快照选项,但您可能应该根据您的用例调整它们。
//CHECK FOR CACHED MAP, IF NOT THEN GENERATE A NEW MAP SNAPSHOT
let coord = CLLocationCoordinate2D(latitude: placeLatitude, longitude: placeLongitude)
let snapshotterOptions = MKMapSnapshotOptions()
snapshotterOptions.scale = UIScreen.mainScreen().scale
let squareDimension = cell.bounds.width < cell.bounds.height ? (cell.bounds.width)! : (cell.bounds.height)!
snapshotterOptions.size = CGSize(width: squareDimension, height: squareDimension)
snapshotterOptions.mapType = MKMapType.Hybrid
snapshotterOptions.showsBuildings = true
snapshotterOptions.showsPointsOfInterest = true
snapshotterOptions.region = MKCoordinateRegionMakeWithDistance(coord, 5000, 5000)
let snapper = MKMapSnapshotter(options: snapshotterOptions)
snapper.startWithCompletionHandler({ (snap, error) in
if(error != nil) {
print("error: " + error!.localizedDescription)
return
}
dispatch_async(dispatch_get_main_queue()) {
guard let snap = snap where error == nil else { return }
if let indexPaths = self.tview.indexPathsForVisibleRows {
if indexPaths.contains(indexPath) {
cell.mapImageView.image = snap
}
}
//SET CACHE HERE
}
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
217 次 |
| 最近记录: |