Skip to content

Instantly share code, notes, and snippets.

@LutherBaker
Last active August 29, 2015 14:01
Show Gist options
  • Save LutherBaker/bd23583175cef6facbd4 to your computer and use it in GitHub Desktop.
Save LutherBaker/bd23583175cef6facbd4 to your computer and use it in GitHub Desktop.
Grand Central Dispatch
// https://developer.apple.com/library/ios/documentation/userexperience/conceptual/LocationAwarenessPG/MapKit/MapKit.html
// Initialize the semaphore to 0 because there are no resources yet.
dispatch_semaphore_t snapshotSem = dispatch_semaphore_create(0);
// Get a global queue (it doesn't matter which one).
dispatch_queue_t queue = dispatch_get_global_queue(myQueuePriorityLevel, 0);
// Create variables to hold return values. Use the __block modifier because these
variables will be modified inside a block.
__block MKMapSnapshot *mapSnapshot = nil;
__block NSError *error = nil;
// Start the asynchronous snapshot-creation task.
[snapshotter startWithQueue:queue
completionHandler:^(MKMapSnapshot *snapshot, NSError *e) {
mapSnapshot = snapshot;
error = e;
// The dispatch_semaphore_signal function tells the semaphore that the async task is finished, which unblocks the main thread.
dispatch_semaphore_signal(snapshotSem);
}];
// On the main thread, use dispatch_semaphore_wait to wait for the snapshot task to complete.
dispatch_semaphore_wait(snapshotSem, DISPATCH_TIME_FOREVER);
if (error) { // Handle error. }
// Get the image from the newly created snapshot.
UIImage *image = mapSnapshot.image;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment