Add base64 option
This commit is contained in:
parent
5c8b7f2fa7
commit
696ea848a1
|
@ -53,6 +53,7 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity
|
||||||
|
|
||||||
private boolean cropping = false;
|
private boolean cropping = false;
|
||||||
private boolean multiple = false;
|
private boolean multiple = false;
|
||||||
|
private boolean includeBase64 = false;
|
||||||
private int width = 100;
|
private int width = 100;
|
||||||
private int height = 100;
|
private int height = 100;
|
||||||
|
|
||||||
|
@ -76,6 +77,7 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity
|
||||||
}
|
}
|
||||||
|
|
||||||
multiple = options.hasKey("multiple") && options.getBoolean("multiple");
|
multiple = options.hasKey("multiple") && options.getBoolean("multiple");
|
||||||
|
includeBase64 = options.hasKey("includeBase64") && options.getBoolean("includeBase64");
|
||||||
width = options.hasKey("width") ? options.getInt("width") : width;
|
width = options.hasKey("width") ? options.getInt("width") : width;
|
||||||
height = options.hasKey("height") ? options.getInt("height") : height;
|
height = options.hasKey("height") ? options.getInt("height") : height;
|
||||||
cropping = options.hasKey("cropping") ? options.getBoolean("cropping") : cropping;
|
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) {
|
private WritableMap getImage(Uri uri, boolean resolvePath) {
|
||||||
WritableMap image = new WritableNativeMap();
|
WritableMap image = new WritableNativeMap();
|
||||||
String path = uri.getPath();
|
String path = uri.getPath();
|
||||||
String data = getBase64StringFromFile(path);
|
|
||||||
|
|
||||||
if (resolvePath) {
|
if (resolvePath) {
|
||||||
path = RealPathUtil.getRealPathFromURI(activity, uri);
|
path = RealPathUtil.getRealPathFromURI(activity, uri);
|
||||||
|
@ -140,12 +141,17 @@ public class PickerModule extends ReactContextBaseJavaModule implements Activity
|
||||||
|
|
||||||
BitmapFactory.decodeFile(path, options);
|
BitmapFactory.decodeFile(path, options);
|
||||||
image.putString("path", "file://" + path);
|
image.putString("path", "file://" + path);
|
||||||
image.putString("data", data);
|
|
||||||
image.putInt("width", options.outWidth);
|
image.putInt("width", options.outWidth);
|
||||||
image.putInt("height", options.outHeight);
|
image.putInt("height", options.outHeight);
|
||||||
image.putString("mime", options.outMimeType);
|
image.putString("mime", options.outMimeType);
|
||||||
image.putInt("size", (int)fileLen);
|
image.putInt("size", (int)fileLen);
|
||||||
|
|
||||||
|
if (includeBase64) {
|
||||||
|
String data = getBase64StringFromFile(path);
|
||||||
|
|
||||||
|
image.putString("data", data);
|
||||||
|
}
|
||||||
|
|
||||||
return image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ RCT_EXPORT_MODULE();
|
||||||
self.defaultOptions = @{
|
self.defaultOptions = @{
|
||||||
@"multiple": @NO,
|
@"multiple": @NO,
|
||||||
@"cropping": @NO,
|
@"cropping": @NO,
|
||||||
|
@"includeBase64": @NO,
|
||||||
@"maxFiles": @5,
|
@"maxFiles": @5,
|
||||||
@"width": @200,
|
@"width": @200,
|
||||||
@"height": @200
|
@"height": @200
|
||||||
|
@ -85,7 +86,6 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
|
||||||
resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
|
resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
|
||||||
UIImage *image = [UIImage imageWithData:imageData];
|
UIImage *image = [UIImage imageWithData:imageData];
|
||||||
NSData *data = UIImageJPEGRepresentation(image, 1);
|
NSData *data = UIImageJPEGRepresentation(image, 1);
|
||||||
NSString *dataString = [data base64EncodedStringWithOptions:0];
|
|
||||||
|
|
||||||
NSString *filePath = [self persistFile:data];
|
NSString *filePath = [self persistFile:data];
|
||||||
if (filePath == nil) {
|
if (filePath == nil) {
|
||||||
|
@ -93,14 +93,22 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
[images addObject:@{
|
NSMutableDictionary *object = [NSMutableDictionary
|
||||||
|
dictionaryWithDictionary:@{
|
||||||
@"path": filePath,
|
@"path": filePath,
|
||||||
@"data": dataString,
|
|
||||||
@"width": @(asset.pixelWidth),
|
@"width": @(asset.pixelWidth),
|
||||||
@"height": @(asset.pixelHeight),
|
@"height": @(asset.pixelHeight),
|
||||||
@"mime": @"image/jpeg",
|
@"mime": @"image/jpeg",
|
||||||
@"size": [NSNumber numberWithUnsignedInteger:data.length]
|
@"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 {
|
} else {
|
||||||
UIImage *image = [UIImage imageWithData:imageData];
|
UIImage *image = [UIImage imageWithData:imageData];
|
||||||
NSData *data = UIImageJPEGRepresentation(image, 1);
|
NSData *data = UIImageJPEGRepresentation(image, 1);
|
||||||
NSString *dataString = [data base64EncodedStringWithOptions:0];
|
|
||||||
NSString *filePath = [self persistFile:data];
|
NSString *filePath = [self persistFile:data];
|
||||||
if (filePath == nil) {
|
if (filePath == nil) {
|
||||||
self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil);
|
self.reject(ERROR_CANNOT_SAVE_IMAGE_KEY, ERROR_CANNOT_SAVE_IMAGE_MSG, nil);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.resolve(@{
|
NSMutableDictionary *object = [NSMutableDictionary
|
||||||
|
dictionaryWithDictionary:@{
|
||||||
@"path": filePath,
|
@"path": filePath,
|
||||||
@"data": dataString,
|
|
||||||
@"width": @(asset.pixelWidth),
|
@"width": @(asset.pixelWidth),
|
||||||
@"height": @(asset.pixelHeight),
|
@"height": @(asset.pixelHeight),
|
||||||
@"mime": @"image/jpeg",
|
@"mime": @"image/jpeg",
|
||||||
@"size": [NSNumber numberWithUnsignedInteger:data.length]
|
@"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];
|
[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]);
|
CGSize resizedImageSize = CGSizeMake([[[self options] objectForKey:@"width"] intValue], [[[self options] objectForKey:@"height"] intValue]);
|
||||||
UIImage *resizedImage = [croppedImage resizedImageToFitInSize:resizedImageSize scaleIfSmaller:YES];
|
UIImage *resizedImage = [croppedImage resizedImageToFitInSize:resizedImageSize scaleIfSmaller:YES];
|
||||||
NSData *data = UIImageJPEGRepresentation(resizedImage, 1);
|
NSData *data = UIImageJPEGRepresentation(resizedImage, 1);
|
||||||
NSString *dataString = [data base64EncodedStringWithOptions:0];
|
|
||||||
|
|
||||||
NSString *filePath = [self persistFile:data];
|
NSString *filePath = [self persistFile:data];
|
||||||
if (filePath == nil) {
|
if (filePath == nil) {
|
||||||
|
@ -241,16 +255,22 @@ RCT_EXPORT_METHOD(openPicker:(NSDictionary *)options
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
NSDictionary *image = @{
|
NSMutableDictionary *image = [NSMutableDictionary
|
||||||
|
dictionaryWithDictionary:@{
|
||||||
@"path": filePath,
|
@"path": filePath,
|
||||||
@"data": dataString,
|
|
||||||
@"width": @(resizedImage.size.width),
|
@"width": @(resizedImage.size.width),
|
||||||
@"height": @(resizedImage.size.height),
|
@"height": @(resizedImage.size.height),
|
||||||
@"mime": @"image/jpeg",
|
@"mime": @"image/jpeg",
|
||||||
@"size": [NSNumber numberWithUnsignedInteger:data.length]
|
@"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];
|
[controller dismissViewControllerAnimated:YES completion:nil];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue