In each cell of the UITableViewController, I have a built-in map. Is there any way to change it to map screenshots?

In my ios swift app, I have a UITableViewController with adding cells dynamically. Each cell has an integrated MKMapView , and I set the center of the map for each cell in different coordinates. I do this by calling this method:

 func centerMapOnLocation(location: CLLocation, map: MKMapView, radius: CLLocationDistance) { let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, radius * 2.0, radius * 2.0) map.setRegion(coordinateRegion, animated: true) } 

inside 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 } 

This is good, and it works, but I believe that with a lot of records there will be memory problems - even now with only couple cells, when the user scrolls the table - each card loads instantly, and I can only imagine how much its power is calculated this is.

So, my question is: is there a way to change the dynamic view of the map to some kind, maybe a screenshot of the actual position of the map? Will it work faster when it comes to many cells?

+5
source share
1 answer

Yes it is possible. To do this, you should switch to using UIImageView (called mapImageView in the code below) and MKMapSnapshotter. To make things really fly, you have to cache images created by MKMapSnapshotter so that they instantly load when the user scrolls back to the cell that was seen before. This approach is subtle for resources since it uses only one global MKMapSnapShotter, not a map for each cell.

Here is the code that I adapted to your situation. I did not include details on how to cache, because it depends on how you want to handle caching in your application. I have used the Haneke Cockaport: https://cocoapods.org/pods/Haneke in the past. In addition, I have installed many of the standard snapshots listed below, but you should probably adapt them to your use case.

 //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 } }) 
+2
source

All Articles