如何在iOS中获取Google Maps的快照

问题描述 投票:0回答:1

在我的iOS应用中,我打算使用以下代码获取Google Maps地图的快照:

 UIGraphicsBeginImageContext(self.mapView.frame.size)
    if let context = UIGraphicsGetCurrentContext() {
      self.mapView.layer.render(in: context)
    }
      self.imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()

但是,返回的图像是黑屏图像,左下方带有Google徽标。我该怎么做才能获得正确的地图图像快照?

ios swift google-maps screenshot google-maps-sdk-ios
1个回答
1
投票

假设mapViewGMSMapView的一个实例,以下应该可以工作

let image = UIGraphicsImageRenderer(size: mapView.bounds.size).image { _ in
    mapView.drawHierarchy(in: mapView.bounds, afterScreenUpdates: true)
}

这当然是假设在调用此代码之前地图已经实际渲染。

© www.soinside.com 2019 - 2024. All rights reserved.