我正在我的APP中实现位置共享,这迫使我根据其纬度和经度相应地生成多个快照。因此,直接使用即可:
- (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);
}];
}
有一个漂亮的NSHipster好文章有关MKMapSnapshotter
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句