【问题标题】:MKMapView to UIImage iOS 7MKMapView 到 UIImage iOS 7
【发布时间】:2013-10-02 01:53:41
【问题描述】:

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

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

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

【问题讨论】:

  • 您是否尝试使用drawViewHierarchyInRect:afterScreenUpdates: 而不是renderInContext:

标签: iphone ios objective-c uiimage mkmapview


【解决方案1】:

来自this SO 发帖:

您可以使用MKMapSnapshotter 并从生成的MKMapSnapshot 中获取图像。看它的讨论WWDC 2013 session video, Putting Map Kit in Perspective。

例如:

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 的解决方案。

【讨论】:

  • 这应该是一个带有指向另一个答案的链接的评论,而不是它的精确副本。
  • 这不考虑 MKMapView 覆盖。如何在 UIImage 中包含 MKPolyLines 和叠加层?
  • 我的完成块永远不会被调用
猜你喜欢
  • 2011-08-21
  • 2013-12-09
  • 1970-01-01
  • 1970-01-01
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多