将 GMSPanoramaView 转换为 UIImage?

Convert GMSPanoramaView to UIImage?

对于我的应用程序,我需要从 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 街景中获取 static UIImages

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() 并存储您的图像。

https://developers.google.com/maps/documentation/ios-sdk/reference/protocol_g_m_s_panorama_view_delegate-p