如何在 iOS 中获取 Google 地图的快照
How to obtain a snapshot of Google Maps in iOS
在我的 iOS 应用程序中,我打算使用以下代码获取我的 Google 地图地图的快照:
UIGraphicsBeginImageContext(self.mapView.frame.size)
if let context = UIGraphicsGetCurrentContext() {
self.mapView.layer.render(in: context)
}
self.imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
但是,返回的图像是黑屏图像,左下角带有 Google 徽标。我该怎么做才能获得正确的地图图像快照?
假设 mapView
是 GMSMapView
的一个实例,以下应该有效
let image = UIGraphicsImageRenderer(size: mapView.bounds.size).image { _ in
mapView.drawHierarchy(in: mapView.bounds, afterScreenUpdates: true)
}
当然,这是假设在调用此代码之前地图已经实际呈现。
在我的 iOS 应用程序中,我打算使用以下代码获取我的 Google 地图地图的快照:
UIGraphicsBeginImageContext(self.mapView.frame.size)
if let context = UIGraphicsGetCurrentContext() {
self.mapView.layer.render(in: context)
}
self.imageSnapshot = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
但是,返回的图像是黑屏图像,左下角带有 Google 徽标。我该怎么做才能获得正确的地图图像快照?
假设 mapView
是 GMSMapView
的一个实例,以下应该有效
let image = UIGraphicsImageRenderer(size: mapView.bounds.size).image { _ in
mapView.drawHierarchy(in: mapView.bounds, afterScreenUpdates: true)
}
当然,这是假设在调用此代码之前地图已经实际呈现。