2013-09-26 24 views
1

将MKMapView渲染到UIImage的代码不再适用于iOS 7.它返回一个空白图像,其底部只有单词“Legal”,右上角是黑色指南针。地图本身缺失。以下是我的代码:MKMapView到UIImage iOS 7

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

Map是一个指向MKMapView的IBOutlet。有没有办法在iOS 7中正确呈现MKMapView?

+0

您是否尝试使用'drawViewHierarchyInRect:afterScreenUpdates:'而不是'renderInContext:'? –

回答

4

this SO后:

您可以使用MKMapSnapshotter从所得MKMapSnapshot抓取图像。请参阅关于它的讨论WWDC 2013会话视频,将地图工具包放在透视图中。

例如:

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

+1

这应该是一个带有指向其他答案的链接的评论,而不仅仅是一个确切的副本。 – Anna

+2

这没有考虑到MKMapView叠加。我将如何将MKPolyLines和叠加层包含在UIImage中? – smartfuse

+1

我的完成块永远不会被调用 – OMGPOP