gpt4 book ai didi

ios - 如何为 MKPolygonRenderer 提供图像?

转载 作者:行者123 更新时间:2023-11-30 11:21:25 29 4
gpt4 key购买 nike

我的代码正确获取图像并提供 map View 的边界。我想将图像放在边界内,但我不知道如何。我可以用颜色填充它,但我需要帮助使其成为图像。

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
let renderer = MKPolygonRenderer(overlay: overlay)
//renderer.fillColor = UIColor.red -> works
return renderer
}

如何用我下载的图片填充它? https://radar.weather.gov/ridge/Conus/RadarImg/latest_radaronly.gif

最佳答案

使用 MKOverlayRenderer 的子类,您将能够在实际 MKPolygon 上渲染 UIImage 叠加层

class CustomOverlayRenderer: MKOverlayRenderer  {

private let overlayImage: UIImage?

init(overlay: MKOverlay, image: UIImage? = nil) {
self.overlayImage = image
super.init(overlay: overlay)
}

override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
guard let cgImage = self.overlayImage?.cgImage else { return }

let rect = self.rect(for: overlay.boundingMapRect)

context.scaleBy(x: 1.0, y: -1.0)
context.translateBy(x: 0, y: -rect.height)
context.draw(cgImage, in: rect)
}

}

只需将您的委托(delegate)方法更改为使用 CustomOverlayRenderer,如下所示:

func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
return CustomOverlayRenderer(overlay: overlay, image: UIImage(named: "latest_radaronly.gif")!)
}

关于ios - 如何为 MKPolygonRenderer 提供图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51228704/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com