iosmkmapviewmkmapsnapshotter

Create multiple MKMapView snapshots serially


I wrote and use THIS CLASS to create small snapshot images of MKMapView with a custom annotation, and set them inside an UIImageView in a UITableViewCell. I wrote it in such a manner, to invoke and forget, without worrying too much about memory consumption.

How it works:

The Problem

The process of map creation, and setting the completed image to the image view is absolutely fine. However, when more than 1 cell in the table need to plot these different snapshots, each cell creates a new instance of CellMapFactory, which in turn adds a new MKMapView to the app window, for each snapshot requested. This adds insanely to the memory usage.

Desired Solution

I would like CellMapFactory to create these snapshots serially. For instance if 20 cells in my table need 20 unique snapshots, CellMapFactory must process them in a serial manner (queue?), and create one snapshot at a time, using the same MKMapView. The way I see it (maybe I'm wrong), this will help in limiting the memory consumption greatly.

Here's a sample screenshot, just for the record:

enter image description here

Any help (and variations of the desired solution) will be greatly appreciated. Thanks.


Solution

  • How about creating a snapshot interface which your cells can use to request a snapshot. For when the snapshot is not available, it provides a mechanism for you to schedule the snapshot creation and register a delegate or completion block it can call back on to receive the created image.

    If you do this within each cell you could provide a place holder image which will update to the proper image as they become available.

    In the implementation simply queue up the requests which should allow you to do them one at a time. As they complete, cache and fire the completion delegate/block. Not sure how much you can run in the background for this task? Potentially you could run a number of threads which would let you control how many MKMapViews are used and thus balance memory with performance.