9

I am facing a weird problem with MKMapView. I have used a MKOverlayRenderer. Now the problem is when I am zooming out image showing correctly. But in case of zoom in, some portion of the image are cutting off. It's looking like a portion of MapView is coming above the overlay. Following is my overlay renderer code.

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)
    }
}

Though I don't know the actual reason but when I am commenting ctx.rotate(by:) function this problem is been fixed. But that's not my solution cause image has to be in position.

zoom out zoom in

Tapas Pal
  • 6,566
  • 6
  • 32
  • 76

1 Answers1

-1

Please Try below.

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();
}
    }
Abu Ul Hassan
  • 1,076
  • 8
  • 24