Skip to content

Commit 07994f0

Browse files
brunsychristopherdro
authored andcommitted
added missing parameters to google map screenshot (#1774)
1 parent 9b6946a commit 07994f0

File tree

1 file changed

+47
-16
lines changed

1 file changed

+47
-16
lines changed

lib/ios/AirGoogleMaps/AIRGoogleMapManager.m

+47-16
Original file line numberDiff line numberDiff line change
@@ -231,34 +231,65 @@ - (UIView *)view
231231
withWidth:(nonnull NSNumber *)width
232232
withHeight:(nonnull NSNumber *)height
233233
withRegion:(MKCoordinateRegion)region
234+
format:(nonnull NSString *)format
235+
quality:(nonnull NSNumber *)quality
236+
result:(nonnull NSString *)result
234237
withCallback:(RCTResponseSenderBlock)callback)
235238
{
239+
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
240+
NSString *pathComponent = [NSString stringWithFormat:@"Documents/snapshot-%.20lf.%@", timeStamp, format];
241+
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent: pathComponent];
242+
236243
[self.bridge.uiManager addUIBlock:^(__unused RCTUIManager *uiManager, NSDictionary<NSNumber *, UIView *> *viewRegistry) {
237244
id view = viewRegistry[reactTag];
238245
if (![view isKindOfClass:[AIRGoogleMap class]]) {
239-
RCTLogError(@"Invalid view returned from registry, expecting AIRMap, got: %@", view);
246+
RCTLogError(@"Invalid view returned from registry, expecting AIRMap, got: %@", view);
240247
} else {
241248
AIRGoogleMap *mapView = (AIRGoogleMap *)view;
242-
249+
243250
// TODO: currently we are ignoring width, height, region
244-
251+
245252
UIGraphicsBeginImageContextWithOptions(mapView.frame.size, YES, 0.0f);
246253
[mapView.layer renderInContext:UIGraphicsGetCurrentContext()];
247254
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
248-
UIGraphicsEndImageContext();
249-
250-
NSTimeInterval timeStamp = [[NSDate date] timeIntervalSince1970];
251-
NSString *pathComponent = [NSString stringWithFormat:@"Documents/snapshot-%.20lf.png", timeStamp];
252-
NSString *filePath = [NSHomeDirectory() stringByAppendingPathComponent: pathComponent];
253-
254-
NSData *data = UIImagePNGRepresentation(image);
255-
[data writeToFile:filePath atomically:YES];
256-
NSDictionary *snapshotData = @{
257-
@"uri": filePath,
258-
@"data": [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn]
259-
};
260-
callback(@[[NSNull null], snapshotData]);
255+
256+
NSData *data;
257+
if ([format isEqualToString:@"png"]) {
258+
data = UIImagePNGRepresentation(image);
259+
260+
}
261+
else if([format isEqualToString:@"jpg"]) {
262+
data = UIImageJPEGRepresentation(image, quality.floatValue);
263+
}
264+
265+
if ([result isEqualToString:@"file"]) {
266+
[data writeToFile:filePath atomically:YES];
267+
callback(@[[NSNull null], filePath]);
268+
}
269+
else if ([result isEqualToString:@"base64"]) {
270+
callback(@[[NSNull null], [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn]]);
271+
}
272+
else if ([result isEqualToString:@"legacy"]) {
273+
274+
// In the initial (iOS only) implementation of takeSnapshot,
275+
// both the uri and the base64 encoded string were returned.
276+
// Returning both is rarely useful and in fact causes a
277+
// performance penalty when only the file URI is desired.
278+
// In that case the base64 encoded string was always marshalled
279+
// over the JS-bridge (which is quite slow).
280+
// A new more flexible API was created to cover this.
281+
// This code should be removed in a future release when the
282+
// old API is fully deprecated.
283+
[data writeToFile:filePath atomically:YES];
284+
NSDictionary *snapshotData = @{
285+
@"uri": filePath,
286+
@"data": [data base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithCarriageReturn]
287+
};
288+
callback(@[[NSNull null], snapshotData]);
289+
}
290+
261291
}
292+
UIGraphicsEndImageContext();
262293
}];
263294
}
264295

0 commit comments

Comments
 (0)