我正在我的应用程序中实现位置共享,这迫使我根据它们的纬度和经度相应地生成多个快照。所以简单地直接使用:
- (UIImage*) renderToImage:(MKMapView*) view
{
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
会给我一个未完成的快照。
我尝试使用以下代码:
- (void)mapViewDidFinishRenderingMap:(MKMapView *)mapView fullyRendered:(BOOL)fullyRendered
{
// Image creation code here
}
但是,即使我知道它已完全加载,我也很难回到设置快照的正确单元格。
那么有没有一些API可以直接从纬度和经度生成快照而不需要这些复杂的过程?提前谢谢你们!
最佳答案
您可以使用MKMapSnapshotter
来实现此目的。它允许您创建以区域为中心的特定位置的 map 图像。
- (void)renderSnapshotForLocation:(CLLocationCoordinate2D)location {
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.size = CGSizeMake(200, 200); // whatever size you need
options.scale = [[UIScreen mainScreen] scale];
// size of region in degrees of latitude and longitude
MKCoordinateSpan span = MKCoordinateSpanMake(0.25, 0.25);
options.region = MKCoordinateRegionMake(location, span);
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithCompletionHandler:^(MKMapSnapshot * _Nullable snapshot, NSError * _Nullable error) {
if(snapshot){
UIImage *mapImage = snapshot.image;
// do what you need with the image
return;
}
NSLog(@"Error rendering snapshot for map at: (%f, %f)", options.region.center.latitude, options.region.center.longitude);
NSLog(@"%@", error);
}];
}
有一个漂亮的good article on NSHipster关于MKMapSnapshotter
。
关于ios - 给定纬度和经度生成快照的最佳方法是什么,真的鼓励跳过 MKMapView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38536444/