Xamarin iOS 7 MapKit SnapShot

A new addition to the MapKit API in iOS 7 is the MKMapSnapShotter class. This class can be used to take a snap shot of map based images. In the following example I’ll show you how to create a snap shot, and save the image to the devices photo album.

Since we covered creating a new map in an earlier post, we’ll skip that part, and dive right into the goodness!

First, you’ll need to create a new instance of MKMapSnapshotOptions, and set the region, scale, and size properties.

      using (var snapShotOptions = new MKMapSnapshotOptions())
      {
          snapShotOptions.Region = _map.Region;
          snapShotOptions.Scale = UIScreen.MainScreen.Scale;
          snapShotOptions.Size = _map.Frame.Size;

Next, we’ll create a new instance of MKMapSnapshotter, and initialize it with the MKMapSnapshotOptions we just created.

using (var snapShot = new MKMapSnapshotter(snapShotOptions))

Finally, we need to call the start method and handle the completion handler. The handler gives us an MKSnapShop, which has an Image property. We can call the SaveToPhotosAlbum method on the Image property to store it to the device’s album.

snapShot.Start((snapshot, error) =>
              {
                  if (error == null)
                  {
                      snapshot.Image.SaveToPhotosAlbum(
                          (uiimage, imgError) =>
                              {
                                  if (imgError == null)
                                  {
                                      new UIAlertView("Image Saved", "Map View Image Saved!", null, "OK", null).Show();
                                  }

ImageImage

The image should then be available in the device’s photo album. I have updated the MapKitDirections Sample on GitHub to include these new changes.