iOS MKMapShapshotter completion block is not always being called

前端 未结 4 1112
有刺的猬
有刺的猬 2020-12-20 13:53

I am trying to use the new iOS7 MKMapSnapshotter to generate a static map image. Whenever my app needs a map, I call the following:

MKMapSnapshotter *snapsho         


        
相关标签:
4条回答
  • 2020-12-20 14:32

    I have the same issue. The reason was in MKMapSnapshotOptions where i set very small region. I set default values for delta longitude and delta latitude as 0.05. Now it works.

    0 讨论(0)
  • 2020-12-20 14:38

    Had the same issue in different iOS versions. The point was that the AppleMaps app did not load the maps properly also. Restarting the device hard got it to work. So I suppose it is an Apple bug.

    0 讨论(0)
  • 2020-12-20 14:51

    This is (or appears to be) a bug in MKMapSnapshotter.

    If network data and WiFi are turned off, the completion handler will not be called (unless there are cached data in the OS - see https://stackoverflow.com/a/5769108/481207 for clearing the cache).

    In fact, the snapshotter appears to block waiting on data. It does not time out or detect that there is no data. After many minutes, e.g. 15 minutes, snapshotter.isLoading = YES. Calling cancel does not cause the completion handler to be called.

    If WiFi or network data are turned back on, subsequent calls to start a (new) snapshotter will call the completion handler.

    This s*cks badly if a variable is set when the snapshotter is started and cleared in the handler, because the variable is never cleared.

    if (!isRendering) {
        isRendering = YES;
    
        [snapshotter startWithCompletionHandler:
         ^(MKMapSnapshot* snapshot, NSError* error) {
             // This may not be called so this code will
             // never run again.
             isRendering = NO;
         }];
    }
    
    0 讨论(0)
  • 2020-12-20 14:57

    This issue is probably arising since snapshotter is not a property, therefore it is getting autoreleased soon after leaving this scope. Retaining snapshotter as a property should fix it.

    0 讨论(0)
提交回复
热议问题