iOS 7 引入了一种新方法来生成 MKMapView 的屏幕截图。现在可以使用新的 MKMapSnapshot API,如下所示:
MKMapView *mapView = [..your mapview..]
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc]init];
options.region = mapView.region;
options.mapType = MKMapTypeStandard;
options.showsBuildings = NO;
options.showsPointsOfInterest = NO;
options.size = CGSizeMake(1000, 500);
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc]initWithOptions:options];
[snapshotter startWithQueue:dispatch_get_main_queue() completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
if( error ) {
NSLog( @"An error occurred: %@", error );
} else {
[UIImagePNGRepresentation( snapshot.image ) writeToFile:@"/Users/<yourAccountName>/map.png" atomically:YES];
}
}];
目前所有的叠加层和注释都不会被渲染。之后您必须自己将它们渲染到生成的快照图像上。提供的 MKMapSnapshot 对象有一个方便的辅助方法来进行坐标和点之间的映射:
CGPoint point = [snapshot pointForCoordinate:locationCoordinate2D];