MKMapView到UIImage iOS 7

sma*_*use 2 iphone objective-c uiimage mkmapview ios

将MKMapView呈现给UIImage的代码在iOS 7中不再有效.它返回一个空图像,底部只有"Legal"字样,右上角则是黑色指南针.地图本身丢失了.以下是我的代码:

UIGraphicsBeginImageContext(map.bounds.size);
[map.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Run Code Online (Sandbox Code Playgroud)

Map是一个指向MKMapView的IBOutlet.有没有办法在iOS 7中正确渲染MKMapView?

Ani*_*nil 5

这篇SO帖子:

您可以使用MKMapSnapshotter并抓取结果中的图像MKMapSnapshot.请参阅WWDC 2013会话视频的讨论,将Map Kit放入透视图中.

例如:

MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = self.mapView.region;
options.scale = [UIScreen mainScreen].scale;
options.size = self.mapView.frame.size;

MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
    UIImage *image = snapshot.image;
    NSData *data = UIImagePNGRepresentation(image);
    [data writeToFile:[self snapshotFilename] atomically:YES];
}];
Run Code Online (Sandbox Code Playgroud)

话虽如此,renderInContext解决方案仍然适用于我.有关于仅在iOS7的主队列中执行此操作的说明,但它似乎仍然有效.但MKMapSnapshotter似乎更适合iOS7的解决方案.

  • 这不考虑MKMapView叠加.我如何在UIImage中包含MKPolyLines和叠加层? (2认同)