Commit 8796a035 authored by cookiej's avatar cookiej

支持是否返回base64设置 & 返回图片大小

parent f1369e32
...@@ -41,6 +41,8 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule { ...@@ -41,6 +41,8 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
private Promise mPickerPromise; // 保存Promise private Promise mPickerPromise; // 保存Promise
private ReadableMap cameraOptions; // 保存图片选择/相机选项
public RNSyanImagePickerModule(ReactApplicationContext reactContext) { public RNSyanImagePickerModule(ReactApplicationContext reactContext) {
super(reactContext); super(reactContext);
this.reactContext = reactContext; this.reactContext = reactContext;
...@@ -54,23 +56,26 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule { ...@@ -54,23 +56,26 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
@ReactMethod @ReactMethod
public void showImagePicker(ReadableMap options, Callback callback) { public void showImagePicker(ReadableMap options, Callback callback) {
this.cameraOptions = options;
this.mPickerPromise = null; this.mPickerPromise = null;
this.mPickerCallback = callback; this.mPickerCallback = callback;
this.openImagePicker(options); this.openImagePicker();
} }
@ReactMethod @ReactMethod
public void asyncShowImagePicker(ReadableMap options, Promise promise) { public void asyncShowImagePicker(ReadableMap options, Promise promise) {
this.cameraOptions = options;
this.mPickerCallback = null; this.mPickerCallback = null;
this.mPickerPromise = promise; this.mPickerPromise = promise;
this.openImagePicker(options); this.openImagePicker();
} }
@ReactMethod @ReactMethod
public void openCamera(ReadableMap options, Callback callback) { public void openCamera(ReadableMap options, Callback callback) {
this.cameraOptions = options;
this.mPickerPromise = null; this.mPickerPromise = null;
this.mPickerCallback = callback; this.mPickerCallback = callback;
this.openCamera(options); this.openCamera();
} }
/** /**
...@@ -85,19 +90,18 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule { ...@@ -85,19 +90,18 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
/** /**
* 打开相册选择 * 打开相册选择
* @param options 相册参数
*/ */
private void openImagePicker(ReadableMap options) { private void openImagePicker() {
int imageCount = options.getInt("imageCount"); int imageCount = this.cameraOptions.getInt("imageCount");
boolean isCamera = options.getBoolean("isCamera"); boolean isCamera = this.cameraOptions.getBoolean("isCamera");
boolean isCrop = options.getBoolean("isCrop"); boolean isCrop = this.cameraOptions.getBoolean("isCrop");
int CropW = options.getInt("CropW"); int CropW = this.cameraOptions.getInt("CropW");
int CropH = options.getInt("CropH"); int CropH = this.cameraOptions.getInt("CropH");
boolean isGif = options.getBoolean("isGif"); boolean isGif = this.cameraOptions.getBoolean("isGif");
boolean showCropCircle = options.getBoolean("showCropCircle"); boolean showCropCircle = this.cameraOptions.getBoolean("showCropCircle");
boolean showCropFrame = options.getBoolean("showCropFrame"); boolean showCropFrame = this.cameraOptions.getBoolean("showCropFrame");
boolean showCropGrid = options.getBoolean("showCropGrid"); boolean showCropGrid = this.cameraOptions.getBoolean("showCropGrid");
int quality = options.getInt("quality"); int quality = this.cameraOptions.getInt("quality");
int modeValue; int modeValue;
if (imageCount == 1) { if (imageCount == 1) {
...@@ -144,16 +148,15 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule { ...@@ -144,16 +148,15 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
/** /**
* 打开相机 * 打开相机
* @param options
*/ */
private void openCamera(ReadableMap options) { private void openCamera() {
boolean isCrop = options.getBoolean("isCrop"); boolean isCrop = this.cameraOptions.getBoolean("isCrop");
int CropW = options.getInt("CropW"); int CropW = this.cameraOptions.getInt("CropW");
int CropH = options.getInt("CropH"); int CropH = this.cameraOptions.getInt("CropH");
boolean showCropCircle = options.getBoolean("showCropCircle"); boolean showCropCircle = this.cameraOptions.getBoolean("showCropCircle");
boolean showCropFrame = options.getBoolean("showCropFrame"); boolean showCropFrame = this.cameraOptions.getBoolean("showCropFrame");
boolean showCropGrid = options.getBoolean("showCropGrid"); boolean showCropGrid = this.cameraOptions.getBoolean("showCropGrid");
int quality = options.getInt("quality"); int quality = this.cameraOptions.getInt("quality");
Activity currentActivity = getCurrentActivity(); Activity currentActivity = getCurrentActivity();
PictureSelector.create(currentActivity) PictureSelector.create(currentActivity)
...@@ -205,10 +208,11 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule { ...@@ -205,10 +208,11 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray(); byte[] bytes = baos.toByteArray();
//base64 encode //base64 encode
byte[] encode = Base64.encode(bytes,Base64.DEFAULT); if (cameraOptions.getBoolean("enableBase64")) {
String encodeString = new String(encode); byte[] encode = Base64.encode(bytes,Base64.DEFAULT);
aImage.putString("base64", encodeString); String encodeString = new String(encode);
aImage.putString("base64", encodeString);
}
} else { } else {
// 压缩过,取 media.getCompressPath(); // 压缩过,取 media.getCompressPath();
BitmapFactory.decodeFile(media.getCompressPath(), options); BitmapFactory.decodeFile(media.getCompressPath(), options);
...@@ -224,9 +228,11 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule { ...@@ -224,9 +228,11 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray(); byte[] bytes = baos.toByteArray();
//base64 encode //base64 encode
byte[] encode = Base64.encode(bytes,Base64.DEFAULT); if (cameraOptions.getBoolean("enableBase64")) {
String encodeString = new String(encode); byte[] encode = Base64.encode(bytes,Base64.DEFAULT);
aImage.putString("base64", encodeString); String encodeString = new String(encode);
aImage.putString("base64", encodeString);
}
} }
if (media.isCut()) { if (media.isCut()) {
...@@ -234,6 +240,8 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule { ...@@ -234,6 +240,8 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
} else { } else {
aImage.putString("original_uri", "file://" + media.getPath()); aImage.putString("original_uri", "file://" + media.getPath());
} }
// TODO: 获取图片size
aImage.putInt("size", 0);
imageList.pushMap(aImage); imageList.pushMap(aImage);
} }
......
...@@ -20,7 +20,8 @@ const defaultOptions = { ...@@ -20,7 +20,8 @@ const defaultOptions = {
circleCropRadius: width/2, // 圆形裁剪半径,默认屏幕宽度一半 circleCropRadius: width/2, // 圆形裁剪半径,默认屏幕宽度一半
showCropFrame: true, // 是否显示裁剪区域,默认true showCropFrame: true, // 是否显示裁剪区域,默认true
showCropGrid: false, // 是否隐藏裁剪区域网格,默认false showCropGrid: false, // 是否隐藏裁剪区域网格,默认false
quality: 90 // 压缩质量 quality: 90, // 压缩质量
enableBase64: false, // 是否返回base64编码,默认不返回
}; };
export default { export default {
......
...@@ -30,25 +30,26 @@ RCT_EXPORT_MODULE() ...@@ -30,25 +30,26 @@ RCT_EXPORT_MODULE()
RCT_EXPORT_METHOD(showImagePicker:(NSDictionary *)options RCT_EXPORT_METHOD(showImagePicker:(NSDictionary *)options
callback:(RCTResponseSenderBlock)callback) { callback:(RCTResponseSenderBlock)callback) {
self.cameraOptions = options;
self.callback = callback; self.callback = callback;
self.resolveBlock = nil; self.resolveBlock = nil;
self.rejectBlock = nil; self.rejectBlock = nil;
[self openImagePickerWithOptions:options]; [self openImagePicker];
} }
RCT_REMAP_METHOD(asyncShowImagePicker, RCT_REMAP_METHOD(asyncShowImagePicker,
options:(NSDictionary *)options options:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) { rejecter:(RCTPromiseRejectBlock)reject) {
self.cameraOptions = options;
self.resolveBlock = resolve; self.resolveBlock = resolve;
self.rejectBlock = reject; self.rejectBlock = reject;
self.callback = nil; self.callback = nil;
[self openImagePickerWithOptions:options]; [self openImagePicker];
} }
RCT_EXPORT_METHOD(openCamera:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) { RCT_EXPORT_METHOD(openCamera:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) {
self.cameraOptions = options; self.cameraOptions = options;
self.callback = callback; self.callback = callback;
self.resolveBlock = nil; self.resolveBlock = nil;
self.rejectBlock = nil; self.rejectBlock = nil;
...@@ -60,17 +61,17 @@ RCT_EXPORT_METHOD(deleteCache) { ...@@ -60,17 +61,17 @@ RCT_EXPORT_METHOD(deleteCache) {
[fileManager removeItemAtPath: [NSString stringWithFormat:@"%@ImageCaches", NSTemporaryDirectory()] error:nil]; [fileManager removeItemAtPath: [NSString stringWithFormat:@"%@ImageCaches", NSTemporaryDirectory()] error:nil];
} }
- (void)openImagePickerWithOptions:(NSDictionary *)options { - (void)openImagePicker {
// 照片最大可选张数 // 照片最大可选张数
NSInteger imageCount = [options sy_integerForKey:@"imageCount"]; NSInteger imageCount = [self.cameraOptions sy_integerForKey:@"imageCount"];
// 显示内部拍照按钮 // 显示内部拍照按钮
BOOL isCamera = [options sy_boolForKey:@"isCamera"]; BOOL isCamera = [self.cameraOptions sy_boolForKey:@"isCamera"];
BOOL isCrop = [options sy_boolForKey:@"isCrop"]; BOOL isCrop = [self.cameraOptions sy_boolForKey:@"isCrop"];
BOOL isGif = [options sy_boolForKey:@"isGif"]; BOOL isGif = [self.cameraOptions sy_boolForKey:@"isGif"];
BOOL showCropCircle = [options sy_boolForKey:@"showCropCircle"]; BOOL showCropCircle = [self.cameraOptions sy_boolForKey:@"showCropCircle"];
NSInteger CropW = [options sy_integerForKey:@"CropW"]; NSInteger CropW = [self.cameraOptions sy_integerForKey:@"CropW"];
NSInteger CropH = [options sy_integerForKey:@"CropH"]; NSInteger CropH = [self.cameraOptions sy_integerForKey:@"CropH"];
NSInteger circleCropRadius = [options sy_integerForKey:@"circleCropRadius"]; NSInteger circleCropRadius = [self.cameraOptions sy_integerForKey:@"circleCropRadius"];
NSInteger quality = [self.cameraOptions sy_integerForKey:@"quality"]; NSInteger quality = [self.cameraOptions sy_integerForKey:@"quality"];
TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:imageCount delegate:nil]; TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:imageCount delegate:nil];
...@@ -256,21 +257,25 @@ RCT_EXPORT_METHOD(deleteCache) { ...@@ -256,21 +257,25 @@ RCT_EXPORT_METHOD(deleteCache) {
- (NSDictionary *)handleImageData:(UIImage *) image quality:(NSInteger)quality { - (NSDictionary *)handleImageData:(UIImage *) image quality:(NSInteger)quality {
NSMutableDictionary *photo = [NSMutableDictionary dictionary]; NSMutableDictionary *photo = [NSMutableDictionary dictionary];
NSData *imageData = UIImageJPEGRepresentation(image, quality * 1.0 / 100);
// 剪切图片并放在tmp中 // 剪切图片并放在tmp中
photo[@"width"] = @(image.size.width); photo[@"width"] = @(image.size.width);
photo[@"height"] = @(image.size.height); photo[@"height"] = @(image.size.height);
photo[@"size"] = @(imageData.length);
NSString *fileName = [NSString stringWithFormat:@"%@.jpg", [[NSUUID UUID] UUIDString]]; NSString *fileName = [NSString stringWithFormat:@"%@.jpg", [[NSUUID UUID] UUIDString]];
[self createDir]; [self createDir];
NSString *filePath = [NSString stringWithFormat:@"%@ImageCaches/%@", NSTemporaryDirectory(), fileName]; NSString *filePath = [NSString stringWithFormat:@"%@ImageCaches/%@", NSTemporaryDirectory(), fileName];
if ([UIImageJPEGRepresentation(image, quality/100) writeToFile:filePath atomically:YES]) { if ([imageData writeToFile:filePath atomically:YES]) {
photo[@"uri"] = filePath; photo[@"uri"] = filePath;
} else { } else {
NSLog(@"保存压缩图片失败%@", filePath); NSLog(@"保存压缩图片失败%@", filePath);
} }
NSData *data = UIImageJPEGRepresentation(image, quality/100);
NSString *dataString = [data base64EncodedStringWithOptions:0]; // base64 encoded image string if ([self.cameraOptions sy_boolForKey:@"enableBase64"]) {
photo[@"base64"] = dataString; photo[@"base64"] = [imageData base64EncodedStringWithOptions:0];
}
return photo; return photo;
} }
...@@ -307,7 +312,6 @@ RCT_EXPORT_METHOD(deleteCache) { ...@@ -307,7 +312,6 @@ RCT_EXPORT_METHOD(deleteCache) {
} }
- (UIViewController *)topViewController { - (UIViewController *)topViewController {
// UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
UIViewController *rootViewController = RCTPresentedViewController(); UIViewController *rootViewController = RCTPresentedViewController();
return rootViewController; return rootViewController;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment