ios - 给定纬度和经度生成快照的最佳方法是什么,真的鼓励跳过 MKMapView

标签 ios objective-c mkmapview snapshot

我正在我的应用程序中实现位置共享,这迫使我根据它们的纬度和经度相应地生成多个快照。所以简单地直接使用:

- (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/

相关文章:

ios - 如何在 dd/MM/YYYY 中格式化 NSDate

ios - 将 NSDecimalNumber 与 NSNumber 进行比较得到错误的结果

objective-c - NSOutlineView:为什么 shouldEditTableColumn: 被调用两次?

ios - MKPlacemark 图钉标题

ios - objective-c - View 消失时调用 textFieldDidEndEditing

iOS - 无法部署到 iOS 6.1 模拟器或将部署目标设置为 iOS 6.1

iOS MapKit 相机重置 6 到 354 之间的航向值

ios - MKMapView addAnnotations 崩溃

ios - 如何在iOS中使用谷歌图形API在X轴上显示所有值以进行连续系列

ios - 如何使用按钮更改状态栏的颜色?