diff options
author | Ivo van Dongen <info@ivovandongen.nl> | 2017-09-05 14:58:09 +0300 |
---|---|---|
committer | Ivo van Dongen <ivovandongen@users.noreply.github.com> | 2017-09-05 16:29:33 +0300 |
commit | d736429271a199d6af3dbc7c77f6c40a25b6f91f (patch) | |
tree | f6aed27a61cefa26b849b501893de87a0304a48b /platform/macos/app/MapDocument.m | |
parent | b3b9ef5c6c090a5c8ec01ab041c756de54deb289 (diff) | |
download | qtlocation-mapboxgl-d736429271a199d6af3dbc7c77f6c40a25b6f91f.tar.gz |
[macos] snapshotter
Diffstat (limited to 'platform/macos/app/MapDocument.m')
-rw-r--r-- | platform/macos/app/MapDocument.m | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/platform/macos/app/MapDocument.m b/platform/macos/app/MapDocument.m index d6855d3ff2..406ff0ca9d 100644 --- a/platform/macos/app/MapDocument.m +++ b/platform/macos/app/MapDocument.m @@ -3,6 +3,7 @@ #import "AppDelegate.h" #import "LimeGreenStyleLayer.h" #import "DroppedPinAnnotation.h" +#import "MGLMapsnapshotter.h" #import "MGLStyle+MBXAdditions.h" #import "MGLVectorSource+MGLAdditions.h" @@ -73,6 +74,9 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio BOOL _isTouringWorld; BOOL _isShowingPolygonAndPolylineAnnotations; BOOL _isShowingAnimatedAnnotation; + + // Snapshotter + MGLMapSnapshotter* snapshotter; } #pragma mark Lifecycle @@ -153,6 +157,67 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio camera.heading, camera.pitch]]; } +#pragma mark File methods + +- (IBAction)takeSnapshot:(id)sender { + MGLMapCamera *camera = self.mapView.camera; + + MGLMapSnapshotOptions* options = [[MGLMapSnapshotOptions alloc] initWithStyleURL:self.mapView.styleURL camera:camera size:self.mapView.bounds.size]; + options.zoom = self.mapView.zoomLevel; + + // Create and start the snapshotter + snapshotter = [[MGLMapSnapshotter alloc] initWithOptions:options]; + [snapshotter startWithCompletionHandler: ^(NSImage *image, NSError *error) { + if (error) { + NSLog(@"Could not load snapshot: %@", [error localizedDescription]); + } else { + NSWindow* window = [[[self windowControllers] objectAtIndex:0] window]; + + NSString* newName = [[@"snapshot" stringByDeletingPathExtension] stringByAppendingPathExtension:@"png"]; + + // Set the default name for the file and show the panel. + NSSavePanel* panel = [NSSavePanel savePanel]; + [panel setNameFieldStringValue:newName]; + [panel beginSheetModalForWindow:window completionHandler:^(NSInteger result){ + if (result == NSFileHandlingPanelOKButton) { + // Write the contents in the new format. + NSURL* fileURL = [panel URL]; + + NSBitmapImageRep *bitmapRep = nil; + for (NSImageRep *imageRep in [image representations]) { + if ([imageRep isKindOfClass:[NSBitmapImageRep class]]){ + bitmapRep = (NSBitmapImageRep *)imageRep; + break; // stop on first bitmap rep we find + } + } + + if (!bitmapRep) { + bitmapRep = [NSBitmapImageRep imageRepWithData:[image TIFFRepresentation]]; + + } + + NSString *extension = [[fileURL pathExtension] lowercaseString]; + NSBitmapImageFileType fileType; + if ([extension isEqualToString:@"png"]) { + fileType = NSPNGFileType; + } else if ([extension isEqualToString:@"gif"]) { + fileType = NSGIFFileType; + } else if ([extension isEqualToString:@"jpg"] || [extension isEqualToString:@"jpeg"]) { + fileType = NSJPEGFileType; + } else { + fileType = NSTIFFFileType; + } + + NSData *imageData = [bitmapRep representationUsingType:fileType properties:@{}]; + [imageData writeToURL:fileURL atomically:NO]; + } + }]; + + } + snapshotter = nil; + }]; +} + #pragma mark View methods - (IBAction)showStyle:(id)sender { @@ -963,6 +1028,9 @@ NS_ARRAY_OF(id <MGLAnnotation>) *MBXFlattenedShapes(NS_ARRAY_OF(id <MGLAnnotatio if (menuItem.action == @selector(giveFeedback:)) { return YES; } + if (menuItem.action == @selector(takeSnapshot:)) { + return !(snapshotter && [snapshotter isLoading]); + } return NO; } |