Last active
August 29, 2015 13:56
-
-
Save MrZoidberg/8953459 to your computer and use it in GitHub Desktop.
Mapbox background rendering HOW-TO
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//First of all you need to modify your Mapbox SDK with the this commit: | |
//https://github.com/MrZoidberg/mapbox-ios-sdk/commit/2865d642f163b15f939a906337de4f53bd17a7b6 | |
//Otherwise you will have random crashes during background rendering of maps | |
//We need semaphore to limit the number of concurrent threads | |
//Place it one per view | |
dispatch_semaphore_t _concurrencyLimitingSemaphore = dispatch_semaphore_create(3); | |
//Also we need cache to store snapshots of the map | |
NSCache *_imageCache = [NSCache new]; | |
- (RMStaticMapView *)createMapWithInfo:(NSDictionary *)info | |
{ | |
//Create and init RMStaticMapView | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
UITableViewCell *cell = //create and init cell | |
//.... | |
UIImage *mapImage = [_imageCache objectForKey:@(indexPath.row)]; | |
if (mapImage) { | |
cell.mapImage = mapImage; | |
} else { | |
//Initially set cell.mapImage.backgroundColor to [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile6.png"]] | |
cell.mapImage = nil; | |
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ | |
dispatch_semaphore_wait(_concurrencyLimitingSemaphore, DISPATCH_TIME_FOREVER); | |
RMStaticMapView *mapView = [self createMapWithInfo:rowInfo]; | |
UIImage *mapImage = [mapView takeSnapshot]; | |
if (mapImage) | |
{ | |
[_imageCache setObject:@(indexPath.row) forKey:parkingId]; | |
dispatch_async(dispatch_get_main_queue(), ^(void) { | |
[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone]; | |
}); | |
} | |
dispatch_semaphore_signal(_concurrencyLimitingSemaphore); | |
}); | |
} | |
return cell; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing this! Have you tried this code with a recent version of Mapbox? I can't get my static maps to render without adding them to the view tree - and adding them as hidden or with alpha == 0, they will just render a black image. Did you add them to the view hierarchy in
createMapWithInfo
?(This is with an RMMBTilesSource, not the default tiles from Mapbox)