2

I have a MKMapView where the user can choose if he wants to use Apple Maps or an alternative map source. On that map I draw a MkPolyline showing the current heading. That line updates once a second by removing and adding the line. My app does a lot of other calculations, and while in debug mode the CPU level is around 20% when using Apple maps. If I add the MKTileOverlay to use an alternative map source, the CPU level increases to around 140%.

Does anybody know the reason for this? What's different when using a MkTileOverlay?

Zifigo
  • 196
  • 8
  • 1
    I am also centering the map to current position every 1 second using globalVariables.prefMapCenter. CPU usage was dramatically reduced by setting animated to false. But still CPU usage is 2 to 3 times larger when using tile overlays. – Zifigo Sep 05 '18 at 16:49
  • You found the answer? – Aleksandr Maybach Mar 12 '19 at 09:16
  • Hi Aleksandr, no I still have the problem. Do you experience the same? – Zifigo Mar 16 '19 at 14:58
  • Yes, I use tile overlay and draw poly line over then. And VectorKit "CPU level increases to around 140%". Looks like an internal error. – Aleksandr Maybach Mar 17 '19 at 09:25
  • @Zifigo I have seen you have successfully added both MKTileOverlay and MKPolylineOverlay. Mine One overlay gets removed when I add the other one can you help me with that? here is my question https://stackoverflow.com/questions/59389033/prevent-replacing-of-wms-overlay-while-adding-polygon-or-polyline-to-mkmapview – Anirudha Mahale Dec 18 '19 at 09:39
  • @Anirudha - I see you found your error in the code... – Zifigo Dec 19 '19 at 15:52

1 Answers1

0

Had a same problem, took me a while to figure it out but here it is: This will happen when your tile overlay's override func loadTile(at path: MKTileOverlayPath, result: @escaping (Data?, Error?) -> Void) has an error (for example because of url error or image decoding) and the result callback's first parameter is set null. To solve it, I return an empty image data instead. Here is an example:

 class MyTileOverlay: MKTileOverlay {
    private lazy var emptyImageData: Data? = {
        UIGraphicsBeginImageContext(tileSize)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image?.pngData()
    }()

    override func loadTile(at path: MKTileOverlayPath, result: @escaping (Data?, Error?) -> Void) {
        super.loadTile(at: path) { (data, error) in
            if let data = data,
               var image = UIImage(data: data){
    
                if UIScreen.main.traitCollection.userInterfaceStyle == .dark {
                    image = image.invert()
                }
                result(image.pngData(), error)
            }else {
                result(self.emptyImageData, error)
            }
        }
    }
piotros
  • 23
  • 4