2

将 MKMapView 渲染到 UIImage 的代码在 iOS 7 中不再有效。它返回一个空图像,底部只有单词“Legal”,右上角有一个黑色指南针。地图本身不见了。下面是我的代码:

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

Map 是一个指向 MKMapView 的 IBOutlet。有什么方法可以在 iOS 7 中正确呈现 MKMapView?

4

1 回答 1

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];
}];

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

于 2013-09-26T07:59:56.300 回答