svguerin3 svguerin3 - 3 months ago 20x
iOS Question

MKMapSnapshotter memory crash - multiple calls in a UITableView

We have an iOS app that has a UITableView containing map snapshots that are generated when the cells appear. The sample list we are using is simply showing the map snapshots based on a lat/long provided in a Model class. I started noticing memory crashes, so I reduced the code to its bare minimum. The crashes still occur, when we are only doing the snapshot with nothing even being done to the results. See below for the code, which is contained in our custom cell and called via the


private func testMapSnapshot(viewModel: StreamViewModel)
let latDelta:CLLocationDegrees = 0.005
let lonDelta:CLLocationDegrees = 0.005

let span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
let location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(viewModel.coordinate.latitude, viewModel.coordinate.longitude)
let region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)

let options = MKMapSnapshotOptions()
options.region = region
options.size = mapImageView.frame.size
options.scale = UIScreen.mainScreen().scale

viewModel.mapSnapshotter = MKMapSnapshotter(options: options)
viewModel.mapSnapshotter!.startWithCompletionHandler() { snapshot, error in
// do nothing

, I am making sure to cancel the mapSnapshotter. See for reference (we keep the list of models in our main VC class that contains the tableview):

func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath)
let model = viewModel?[indexPath.item] {
model.mapSnapshotter = nil

Note that before doing this last step, it was crashing way earlier. But now, if you start scrolling down the list fast, it starts stuttering and won't stop stuttering. If you go up and down a list of about 150 rows, it'll take less than 30 seconds before we start seeing memory warnings and then a crash.

I ran this through Instruments, but it wasn't very helpful. It looks as if the Heap and Anonymous VM allocations are gradually going up, possibly causing the crash. See for reference:

Instruments Screenshot

I saw this post out there:
MKMapSnapshotter uses incredible amounts of CPU & RAM But it's unanswered and doesn't really address why the memory wouldn't be released.

Any thoughts on where to go on this? Thank you in advance, and please let me know if I can provide more info.


Although I was not able to find a fix to this particular issue, I was able to resolve it by calling the map snapshotter only when the scrollView stops - it then grabs all the visible cells and loads only for those. That way it minimizes the number of calls to this API and prevents memory issues, as opposed to it constantly calling it as you scroll down a list via the cellForRow method.