简体   繁体   中英

Performance issue when drawing MKPolyLine over MKTileOverlay

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM