iosmkmapviewmapkitairprintuiprintformatter

Offscreen iOS 6 MKMapViews and UIViewPrintFormatter — getting them to render in time?


I'm interested in printing some MKMapViews that are not part of the view hierarchy, and are created only when the user hits Print.

It seems that the following things are true:

  1. Offscreen MKMapViews do not render at all.
  2. Printing a MKMapView prints it rendered however it currently is.

So it seems that to print my map views, I need to get them into the view hierarchy. Luckily, from experimentation it seems that I can put a bunch of map views in front of each other on the screen and they'll still render.

My main question is how do I know they have rendered? If I just fire off the print command immediately after putting the map views in the view hierarchy, they'll print partially rendered, with missing bits, as shown below. I'd like to wait until they have finished loading, then run the print job. (Even better if they can be loading in the background while the print sheet is on screen, and then I just give the user a progress bar after they hit Print until the maps have finished loading.)

(Below: an image showing what happens if you print a partially loaded map view.)

Printout

My secondary question is: is there a better way to print multiple map views? Getting everything into the view hierarchy is not great — it limits the maximum size I can use, and it means I have a lot of awkward code to juggle subviews.


Solution

  • You should set a delegate (MKMapViewDelegate) on your map views and implement mapViewDidFinishLoadingMap:, which will be called when the map view has finished loading the necessary tiles.