从MKMapView制作UIImage

Lor*_*son 8 mapkit ios

我想创建一个UIImage来自MKMapView.我的地图在视图中正确显示,但UIImage生成的只是一个灰色图像.这是相关的片段.

UIGraphicsBeginImageContext(mapView.bounds.size);
[mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
mapImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

有谁知道如何UIImage使用MapKit?

Prc*_*ela 8

我使用与ios sdk 4.1测试相同的代码并且工作正常.因此,当地图已经显示给用户并且用户按下按钮时,将调用此操作:

UIImage *image = [mapView renderToImage];
Run Code Online (Sandbox Code Playgroud)

这是作为UIView扩展实现的包装函数:

- (UIImage*) renderToImage
{
  UIGraphicsBeginImageContext(self.frame.size);
  [self.layer renderInContext:UIGraphicsGetCurrentContext()];
  UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  UIGraphicsEndImageContext();  
  return image;
}
Run Code Online (Sandbox Code Playgroud)

所以,问题不在于代码部分.