Skip to content

Commit

Permalink
支持是否返回base64设置 & 返回图片大小
Browse files Browse the repository at this point in the history
  • Loading branch information
ljunb committed Mar 30, 2018
1 parent f1369e3 commit 8796a03
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 49 deletions.
70 changes: 39 additions & 31 deletions android/src/main/java/com/reactlibrary/RNSyanImagePickerModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {

private Promise mPickerPromise; // 保存Promise

private ReadableMap cameraOptions; // 保存图片选择/相机选项

public RNSyanImagePickerModule(ReactApplicationContext reactContext) {
super(reactContext);
this.reactContext = reactContext;
Expand All @@ -54,23 +56,26 @@ public String getName() {

@ReactMethod
public void showImagePicker(ReadableMap options, Callback callback) {
this.cameraOptions = options;
this.mPickerPromise = null;
this.mPickerCallback = callback;
this.openImagePicker(options);
this.openImagePicker();
}

@ReactMethod
public void asyncShowImagePicker(ReadableMap options, Promise promise) {
this.cameraOptions = options;
this.mPickerCallback = null;
this.mPickerPromise = promise;
this.openImagePicker(options);
this.openImagePicker();
}

@ReactMethod
public void openCamera(ReadableMap options, Callback callback) {
this.cameraOptions = options;
this.mPickerPromise = null;
this.mPickerCallback = callback;
this.openCamera(options);
this.openCamera();
}

/**
Expand All @@ -85,19 +90,18 @@ public void deleteCache() {

/**
* 打开相册选择
* @param options 相册参数
*/
private void openImagePicker(ReadableMap options) {
int imageCount = options.getInt("imageCount");
boolean isCamera = options.getBoolean("isCamera");
boolean isCrop = options.getBoolean("isCrop");
int CropW = options.getInt("CropW");
int CropH = options.getInt("CropH");
boolean isGif = options.getBoolean("isGif");
boolean showCropCircle = options.getBoolean("showCropCircle");
boolean showCropFrame = options.getBoolean("showCropFrame");
boolean showCropGrid = options.getBoolean("showCropGrid");
int quality = options.getInt("quality");
private void openImagePicker() {
int imageCount = this.cameraOptions.getInt("imageCount");
boolean isCamera = this.cameraOptions.getBoolean("isCamera");
boolean isCrop = this.cameraOptions.getBoolean("isCrop");
int CropW = this.cameraOptions.getInt("CropW");
int CropH = this.cameraOptions.getInt("CropH");
boolean isGif = this.cameraOptions.getBoolean("isGif");
boolean showCropCircle = this.cameraOptions.getBoolean("showCropCircle");
boolean showCropFrame = this.cameraOptions.getBoolean("showCropFrame");
boolean showCropGrid = this.cameraOptions.getBoolean("showCropGrid");
int quality = this.cameraOptions.getInt("quality");

int modeValue;
if (imageCount == 1) {
Expand Down Expand Up @@ -144,16 +148,15 @@ private void openImagePicker(ReadableMap options) {

/**
* 打开相机
* @param options
*/
private void openCamera(ReadableMap options) {
boolean isCrop = options.getBoolean("isCrop");
int CropW = options.getInt("CropW");
int CropH = options.getInt("CropH");
boolean showCropCircle = options.getBoolean("showCropCircle");
boolean showCropFrame = options.getBoolean("showCropFrame");
boolean showCropGrid = options.getBoolean("showCropGrid");
int quality = options.getInt("quality");
private void openCamera() {
boolean isCrop = this.cameraOptions.getBoolean("isCrop");
int CropW = this.cameraOptions.getInt("CropW");
int CropH = this.cameraOptions.getInt("CropH");
boolean showCropCircle = this.cameraOptions.getBoolean("showCropCircle");
boolean showCropFrame = this.cameraOptions.getBoolean("showCropFrame");
boolean showCropGrid = this.cameraOptions.getBoolean("showCropGrid");
int quality = this.cameraOptions.getInt("quality");

Activity currentActivity = getCurrentActivity();
PictureSelector.create(currentActivity)
Expand Down Expand Up @@ -205,10 +208,11 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray();
//base64 encode
byte[] encode = Base64.encode(bytes,Base64.DEFAULT);
String encodeString = new String(encode);
aImage.putString("base64", encodeString);

if (cameraOptions.getBoolean("enableBase64")) {
byte[] encode = Base64.encode(bytes,Base64.DEFAULT);
String encodeString = new String(encode);
aImage.putString("base64", encodeString);
}
} else {
// 压缩过,取 media.getCompressPath();
BitmapFactory.decodeFile(media.getCompressPath(), options);
Expand All @@ -224,16 +228,20 @@ public void onActivityResult(Activity activity, int requestCode, int resultCode,
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytes = baos.toByteArray();
//base64 encode
byte[] encode = Base64.encode(bytes,Base64.DEFAULT);
String encodeString = new String(encode);
aImage.putString("base64", encodeString);
if (cameraOptions.getBoolean("enableBase64")) {
byte[] encode = Base64.encode(bytes,Base64.DEFAULT);
String encodeString = new String(encode);
aImage.putString("base64", encodeString);
}
}

if (media.isCut()) {
aImage.putString("original_uri", "file://" + media.getCutPath());
} else {
aImage.putString("original_uri", "file://" + media.getPath());
}
// TODO: 获取图片size
aImage.putInt("size", 0);

imageList.pushMap(aImage);
}
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const defaultOptions = {
circleCropRadius: width/2, // 圆形裁剪半径,默认屏幕宽度一半
showCropFrame: true, // 是否显示裁剪区域,默认true
showCropGrid: false, // 是否隐藏裁剪区域网格,默认false
quality: 90 // 压缩质量
quality: 90, // 压缩质量
enableBase64: false, // 是否返回base64编码,默认不返回
};

export default {
Expand Down
38 changes: 21 additions & 17 deletions ios/RNSyanImagePicker.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,26 @@ @implementation RNSyanImagePicker

RCT_EXPORT_METHOD(showImagePicker:(NSDictionary *)options
callback:(RCTResponseSenderBlock)callback) {
self.cameraOptions = options;
self.callback = callback;
self.resolveBlock = nil;
self.rejectBlock = nil;
[self openImagePickerWithOptions:options];
[self openImagePicker];
}

RCT_REMAP_METHOD(asyncShowImagePicker,
options:(NSDictionary *)options
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject) {
self.cameraOptions = options;
self.resolveBlock = resolve;
self.rejectBlock = reject;
self.callback = nil;
[self openImagePickerWithOptions:options];
[self openImagePicker];
}

RCT_EXPORT_METHOD(openCamera:(NSDictionary *)options callback:(RCTResponseSenderBlock)callback) {
self.cameraOptions = options;

self.callback = callback;
self.resolveBlock = nil;
self.rejectBlock = nil;
Expand All @@ -60,17 +61,17 @@ @implementation RNSyanImagePicker
[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 isCrop = [options sy_boolForKey:@"isCrop"];
BOOL isGif = [options sy_boolForKey:@"isGif"];
BOOL showCropCircle = [options sy_boolForKey:@"showCropCircle"];
NSInteger CropW = [options sy_integerForKey:@"CropW"];
NSInteger CropH = [options sy_integerForKey:@"CropH"];
NSInteger circleCropRadius = [options sy_integerForKey:@"circleCropRadius"];
BOOL isCamera = [self.cameraOptions sy_boolForKey:@"isCamera"];
BOOL isCrop = [self.cameraOptions sy_boolForKey:@"isCrop"];
BOOL isGif = [self.cameraOptions sy_boolForKey:@"isGif"];
BOOL showCropCircle = [self.cameraOptions sy_boolForKey:@"showCropCircle"];
NSInteger CropW = [self.cameraOptions sy_integerForKey:@"CropW"];
NSInteger CropH = [self.cameraOptions sy_integerForKey:@"CropH"];
NSInteger circleCropRadius = [self.cameraOptions sy_integerForKey:@"circleCropRadius"];
NSInteger quality = [self.cameraOptions sy_integerForKey:@"quality"];

TZImagePickerController *imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:imageCount delegate:nil];
Expand Down Expand Up @@ -256,21 +257,25 @@ - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)butto

- (NSDictionary *)handleImageData:(UIImage *) image quality:(NSInteger)quality {
NSMutableDictionary *photo = [NSMutableDictionary dictionary];
NSData *imageData = UIImageJPEGRepresentation(image, quality * 1.0 / 100);

// 剪切图片并放在tmp中
photo[@"width"] = @(image.size.width);
photo[@"height"] = @(image.size.height);
photo[@"size"] = @(imageData.length);

NSString *fileName = [NSString stringWithFormat:@"%@.jpg", [[NSUUID UUID] UUIDString]];
[self createDir];
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;
} else {
NSLog(@"保存压缩图片失败%@", filePath);
}
NSData *data = UIImageJPEGRepresentation(image, quality/100);
NSString *dataString = [data base64EncodedStringWithOptions:0]; // base64 encoded image string
photo[@"base64"] = dataString;

if ([self.cameraOptions sy_boolForKey:@"enableBase64"]) {
photo[@"base64"] = [imageData base64EncodedStringWithOptions:0];
}
return photo;
}

Expand Down Expand Up @@ -307,7 +312,6 @@ - (BOOL)createDir {
}

- (UIViewController *)topViewController {
// UIViewController *rootViewController = [[[UIApplication sharedApplication] keyWindow] rootViewController];
UIViewController *rootViewController = RCTPresentedViewController();
return rootViewController;
}
Expand Down

0 comments on commit 8796a03

Please sign in to comment.