Search code examples
uiimagemkmapviewscreenshot

Taking a screenshot of MKMapView


I'm trying to get a screenshot of a MKMapView.

and I'm using the following code:

UIGraphicsBeginImageContext(myMapView.frame.size);

[myMapView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenShot=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return screenShot;

And I'm getting and almost blank image with the map current location icon and a Google logo in it.

What could be causing that?

I should tell you that myMapView is actually on another viewController's view but since I'm getting the blue spot showing the location and the google logo I assume the reference I have is the correct one.

Thank you.


Solution

  • As mentioned here, you can try this

    - (UIImage*) renderToImage
    {
       UIGraphicsBeginImageContextWithOptions(self.frame.size, NO, 0.0);
       [self.layer renderInContext:UIGraphicsGetCurrentContext()];
      UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
      UIGraphicsEndImageContext(); 
      return image;
    }