给定纬度和经度生成快照的最佳方法是什么,真的鼓励跳过 MKMapView
What is the best way to generate a snapshot given latitude and longitude, skipping MKMapView is really encouraged
我正在我的应用程序中实现位置共享,这迫使我根据它们的经纬度相应地生成多个快照。所以干脆直接使用:
- (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
来达到这个目的。它允许您在以区域为中心的特定位置创建地图图像。
- (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);
}];
}
关于MKMapSnapshotter
有一个漂亮的good article on NSHipster。
我正在我的应用程序中实现位置共享,这迫使我根据它们的经纬度相应地生成多个快照。所以干脆直接使用:
- (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
来达到这个目的。它允许您在以区域为中心的特定位置创建地图图像。
- (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);
}];
}
关于MKMapSnapshotter
有一个漂亮的good article on NSHipster。