【问题标题】:MapView overlay is cutting off after zoom inMapView 覆盖在放大后被切断
【发布时间】:2017-06-27 13:18:33
【问题描述】:

MKMapView 遇到了一个奇怪的问题。我使用了MKOverlayRenderer。现在的问题是当我缩小正确显示的图像时。但是在放大的情况下,图像的某些部分会被切断。看起来MapView 的一部分出现在覆盖层之上。以下是我的叠加渲染器代码。

class MapOverlayRenderer: MKOverlayRenderer {
    var overlayImage: UIImage
    var plan: Plan

    init(overlay: MKOverlay, overlayImage: UIImage, plan: Plan) {
        self.overlayImage = overlayImage
        self.plan = plan
        super.init(overlay: overlay)
    }

    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in ctx: CGContext) {
        let theMapRect = overlay.boundingMapRect
        let theRect = rect(for: theMapRect)

        // Rotate around top left corner
        ctx.rotate(by: CGFloat(degreesToRadians(plan.bearing)));

        // Draw the image
        UIGraphicsPushContext(ctx)
        overlayImage.draw(in: theRect, blendMode: CGBlendMode.normal, alpha: 1.0)
        UIGraphicsPopContext();
    }

    func degreesToRadians(_ x:Double) -> Double {
        return (M_PI * x / 180.0)
    }
}

虽然我不知道真正的原因,但是当我评论ctx.rotate(by:) 函数时,这个问题已经得到解决。但这不是我的解决方案,因为图像必须就位。

【问题讨论】:

标签: swift3 mkmapview cgcontext mkoverlay cgcontextdrawimage


【解决方案1】:

请在下面尝试。

override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in ctx: CGContext) {
DispatchQueue.main.async {
        let theMapRect = overlay.boundingMapRect
        let theRect = rect(for: theMapRect)
        // Rotate around top left corner
        ctx.rotate(by: CGFloat(degreesToRadians(plan.bearing)));
        // Draw the image
        UIGraphicsPushContext(ctx)
        overlayImage.draw(in: theRect, blendMode: CGBlendMode.normal, alpha: 1.0)
        UIGraphicsPopContext();
}
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多