在我的iOS应用中,我打算使用以下代码获取Google Maps地图的快照:
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)
}
这当然是假设在调用此代码之前地图已经实际渲染。