@@ -231,34 +231,65 @@ - (UIView *)view
231
231
withWidth:(nonnull NSNumber *)width
232
232
withHeight:(nonnull NSNumber *)height
233
233
withRegion:(MKCoordinateRegion)region
234
+ format:(nonnull NSString *)format
235
+ quality:(nonnull NSNumber *)quality
236
+ result:(nonnull NSString *)result
234
237
withCallback:(RCTResponseSenderBlock)callback)
235
238
{
239
+ NSTimeInterval timeStamp = [[NSDate date ] timeIntervalSince1970 ];
240
+ NSString *pathComponent = [NSString stringWithFormat: @" Documents/snapshot-%.20lf .%@ " , timeStamp, format];
241
+ NSString *filePath = [NSHomeDirectory () stringByAppendingPathComponent: pathComponent];
242
+
236
243
[self .bridge.uiManager addUIBlock: ^(__unused RCTUIManager *uiManager, NSDictionary <NSNumber *, UIView *> *viewRegistry) {
237
244
id view = viewRegistry[reactTag];
238
245
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);
240
247
} else {
241
248
AIRGoogleMap *mapView = (AIRGoogleMap *)view;
242
-
249
+
243
250
// TODO: currently we are ignoring width, height, region
244
-
251
+
245
252
UIGraphicsBeginImageContextWithOptions (mapView.frame .size , YES , 0 .0f );
246
253
[mapView.layer renderInContext: UIGraphicsGetCurrentContext ()];
247
254
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
+
261
291
}
292
+ UIGraphicsEndImageContext ();
262
293
}];
263
294
}
264
295
0 commit comments