From 696ea848a14878bd41ffdb8a5e04a40054b5531c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Je=CC=81re=CC=81my=20Magrin?= Date: Fri, 5 Aug 2016 16:08:16 +0200 Subject: [PATCH] Add base64 option --- .../com/reactnative/picker/PickerModule.java | 10 ++- ios/ImageCropPicker.m | 76 ++++++++++++------- 2 files changed, 56 insertions(+), 30 deletions(-) diff --git a/android/src/main/java/com/reactnative/picker/PickerModule.java b/android/src/main/java/com/reactnative/picker/PickerModule.java index 8849700..8ce27e2 100644 --- a/android/src/main/java/com/reactnative/picker/PickerModule.java +++ b/android/src/main/java/com/reactnative/picker/PickerModule.java @@ -53,6 +53,7 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity private boolean cropping = false; private boolean multiple = false; + private boolean includeBase64 = false; private int width = 100; private int height = 100; @@ -76,6 +77,7 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity } multiple = options.hasKey("multiple") && options.getBoolean("multiple"); + includeBase64 = options.hasKey("includeBase64") && options.getBoolean("includeBase64"); width = options.hasKey("width") ? options.getInt("width") : width; height = options.hasKey("height") ? options.getInt("height") : height; cropping = options.hasKey("cropping") ? options.getBoolean("cropping") : cropping; @@ -124,7 +126,6 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity private WritableMap getImage(Uri uri, boolean resolvePath) { WritableMap image = new WritableNativeMap(); String path = uri.getPath(); - String data = getBase64StringFromFile(path); if (resolvePath) { path = RealPathUtil.getRealPathFromURI(activity, uri); @@ -140,12 +141,17 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity BitmapFactory.decodeFile(path, options); image.putString("path", "file://" + path); - image.putString("data", data); image.putInt("width", options.outWidth); image.putInt("height", options.outHeight); image.putString("mime", options.outMimeType); image.putInt("size", (int)fileLen); + if (includeBase64) { + String data = getBase64StringFromFile(path); + + image.putString("data", data); + } + return image; } diff --git a/ios/ImageCropPicker.m b/ios/ImageCropPicker.m index 0171b6d..5119987 100644 --- a/ios/ImageCropPicker.m +++ b/ios/ImageCropPicker.m @@ -24,6 +24,7 @@ RCT_EXPORT_MODULE(); self.defaultOptions = @{ @"multiple": @NO, @"cropping": @NO, + @"includeBase64": @NO, @"maxFiles": @5, @"width": @200, @"height": @200 @@ -85,7 +86,6 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) { UIImage *image = [UIImage imageWithData:imageData]; NSData *data = UIImageJPEGRepresentation(image, 1); - NSString *dataString = [data base64EncodedStringWithOptions:0]; NSString *filePath = [self persistFile:data]; if (filePath == nil) { @@ -93,14 +93,22 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options return; } - [images addObject:@{ - @"path": filePath, - @"data": dataString, - @"width": @(asset.pixelWidth), - @"height": @(asset.pixelHeight), - @"mime": @"image/jpeg", - @"size": [NSNumber numberWithUnsignedInteger:data.length] - }]; + NSMutableDictionary *object = [NSMutableDictionary + dictionaryWithDictionary:@{ + @"path": filePath, + @"width": @(asset.pixelWidth), + @"height": @(asset.pixelHeight), + @"mime": @"image/jpeg", + @"size": [NSNumber numberWithUnsignedInteger:data.length]}]; + + + if ([[[self options] objectForKey:@"includeBase64"] boolValue]) { + NSString *dataString = [data base64EncodedStringWithOptions:0]; + + object[@"data"] = dataString; + } + + [images addObject:[object copy]]; }]; } @@ -137,21 +145,28 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options } else { UIImage *image = [UIImage imageWithData:imageData]; NSData *data = UIImageJPEGRepresentation(image, 1); - NSString *dataString = [data base64EncodedStringWithOptions:0]; NSString *filePath = [self persistFile:data]; if (filePath == nil) { self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil); return; } - self.resolve(@{ - @"path": filePath, - @"data": dataString, - @"width": @(asset.pixelWidth), - @"height": @(asset.pixelHeight), - @"mime": @"image/jpeg", - @"size": [NSNumber numberWithUnsignedInteger:data.length] - }); + NSMutableDictionary *object = [NSMutableDictionary + dictionaryWithDictionary:@{ + @"path": filePath, + @"width": @(asset.pixelWidth), + @"height": @(asset.pixelHeight), + @"mime": @"image/jpeg", + @"size": [NSNumber numberWithUnsignedInteger:data.length]}]; + + + if ([[[self options] objectForKey:@"includeBase64"] boolValue]) { + NSString *dataString = [data base64EncodedStringWithOptions:0]; + + object[@"data"] = dataString; + } + + self.resolve([object copy]); [imagePickerController dismissViewControllerAnimated:YES completion:nil]; } }]; @@ -233,7 +248,6 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options CGSize resizedImageSize = CGSizeMake([[[self options] objectForKey:@"width"] intValue], [[[self options] objectForKey:@"height"] intValue]); UIImage *resizedImage = [croppedImage resizedImageToFitInSize:resizedImageSize scaleIfSmaller:YES]; NSData *data = UIImageJPEGRepresentation(resizedImage, 1); - NSString *dataString = [data base64EncodedStringWithOptions:0]; NSString *filePath = [self persistFile:data]; if (filePath == nil) { @@ -241,16 +255,22 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options return; } - NSDictionary *image = @{ - @"path": filePath, - @"data": dataString, - @"width": @(resizedImage.size.width), - @"height": @(resizedImage.size.height), - @"mime": @"image/jpeg", - @"size": [NSNumber numberWithUnsignedInteger:data.length] - }; + NSMutableDictionary *image = [NSMutableDictionary + dictionaryWithDictionary:@{ + @"path": filePath, + @"width": @(resizedImage.size.width), + @"height": @(resizedImage.size.height), + @"mime": @"image/jpeg", + @"size": [NSNumber numberWithUnsignedInteger:data.length]}]; - self.resolve(image); + + if ([[[self options] objectForKey:@"includeBase64"] boolValue]) { + NSString *dataString = [data base64EncodedStringWithOptions:0]; + + image[@"data"] = dataString; + } + + self.resolve([image copy]); [controller dismissViewControllerAnimated:YES completion:nil]; }