对于我的应用程序,我需要从gps坐标获取Google街景图像。我知道如何从坐标获得全屏GMSPanoramaView,但我最终需要它成为UIImage。
let panoView = GMSPanoramaView(frame: .zero)
self.view = panoView
panoView.moveNearCoordinate(location.coordinate)
panoView.setAllGesturesEnabled(false)
// can this be converted to a UIImage?
var streetViewImage: UIImage?
streetViewImage = panoView
我看到其他人在子视图中展示了GMSPanoramaView - 这是一个更好的选择吗?或者还有其他方法可以从Google街景中获取静态UIImages 吗?
答案 0 :(得分:0)
public extension GMSPanoramaView {
@objc public func toImage() -> UIImage {
UIGraphicsBeginImageContextWithOptions(bounds.size, false, UIScreen.main.scale)
drawHierarchy(in: self.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext()
return image
}
}
以上代码将截取您的观点。要从网络加载正确的数据(而不是获得黑屏幕截图),您需要实现GMSPanoramaViewDelegate,可能是panoramaView:didMoveToPanorama:将在网络请求完成且图像可见时调用。届时,您可以拨打toImage()
并存储图片。