Image rotation of MKOverlay

Hi, I'm making the map app to insert a custom MKOverlay. I specified the image in Asset catalog as an image of MKOVerlay. However, the overlay image in the map was reversed vertically as shown in the **** figure.

https://share.icloud.com/photos/0ofoyXKRlJnN_1phqRP79WBrw

I confirmed that the instance of UIImage to be rendered in a map had been created by specifying a correct asset name. The following is a part of the class inherits MKOverlayRenderer. I cannot understand the rotation of the image. Please teach me the reason for this phenomenon.

     init(overlay: MKOVerlayWindIcon){
        pOverlay = overlay

        super.init(overlay: pOverlay)

        if let tmpImg = GenerateImage(data: pOverlay.data){
            pImageToRender = tmpImg
        }else{
            os_log(.error, "Failed to generage icon image")
        }
        
    }
    
    override func draw(_ mapRect: MKMapRect, zoomScale: MKZoomScale, in context: CGContext) {
        let rectToRender : CGRect = calcCGRect(for: zoomScale)
        if let imageToReder = pImageToRender?.cgImage{
            context.draw(imageToReder, in: rectToRender)
        }else{
            os_log("Invalid image was passed", log: OSLog.default, type: .debug)
        }
    }
    
    func GenerateImage(data :WindData) -> UIImage?{
        let assetName = "WI_337.5"
        return UIImage.init(named: assetName)
    }

Accepted Reply

CoreGraphics uses a different coordinate system, so what I meant if you verified the orientation of the image with respect to the coordinate system. Please see the Quartz 2D programming guide for background on these coordinate systems.


With respect to your overlay, a common way to adjust for this is to briefly adjust the graphics context coordinate system:

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

Replies

Have you confirmed that the image is the correct orientation when it's loaded into memory?

Thank you for your response. The format of source image is PNG. So I think that the orientation of thumbnail in Asset catalog is correct because PNG format cannot have orientation metadata like EXIF.

CoreGraphics uses a different coordinate system, so what I meant if you verified the orientation of the image with respect to the coordinate system. Please see the Quartz 2D programming guide for background on these coordinate systems.


With respect to your overlay, a common way to adjust for this is to briefly adjust the graphics context coordinate system:

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

Thank you for offering more specific information to resolve my issue !!. I resolved my issue. I didn't have enough knowledge of core graphic to develop this kind of feature on iOS device.