Skip to content

Commit 9db5af0

Browse files
authored
Merge pull request #185 from jearyvon/master
增加compressFocusAlpha参数&修正返回的base64的前缀类型不正确问题
2 parents 34182de + 6aa2513 commit 9db5af0

File tree

6 files changed

+31
-12
lines changed

6 files changed

+31
-12
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,8 @@ showCropCircle | bool | 是 | false | 是否显示圆形裁剪区
169169
circleCropRadius | float | 是 | screenW * 0.5 | 圆形裁剪半径,默认屏幕宽度一半
170170
showCropFrame | bool | 是 | true | 是否显示裁剪区域
171171
showCropGrid | bool | 是 | false | 是否隐藏裁剪区域网格
172+
compress | bool | 是 | true | 是否开启压缩(不开启压缩部分图片属性无法获得
173+
compressFocusAlpha | bool | 是 | false | 压缩时保留图片透明度(开启后png压缩后尺寸会变大但是透明度会保留
172174
quality | int | 是 | 90 | 压缩质量(安卓无效,固定鲁班压缩)
173175
minimumCompressSize | int | 是 | 100 | 小于100kb的图片不压缩(Android)
174176
enableBase64 | bool | 是 | false | 是否返回base64编码,默认不返回

android/src/main/java/com/syanpicker/RNSyanImagePickerModule.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ private void openImagePicker() {
165165
int quality = this.cameraOptions.getInt("quality");
166166
boolean isWeChatStyle = this.cameraOptions.getBoolean("isWeChatStyle");
167167
boolean showSelectedIndex = this.cameraOptions.getBoolean("showSelectedIndex");
168+
boolean compressFocusAlpha = this.cameraOptions.getBoolean("compressFocusAlpha");
168169

169170
int modeValue;
170171
if (imageCount == 1) {
@@ -209,6 +210,7 @@ private void openImagePicker() {
209210
.selectionMedia(selectList) // 当前已选中的图片 List
210211
.isWeChatStyle(isWeChatStyle)
211212
.theme(showSelectedIndex ? R.style.picture_WeChat_style : 0)
213+
.compressFocusAlpha(compressFocusAlpha)
212214
.forResult(PictureConfig.CHOOSE_REQUEST); //结果回调onActivityResult code
213215
}
214216

@@ -230,6 +232,7 @@ private void openCamera() {
230232
int quality = this.cameraOptions.getInt("quality");
231233
boolean isWeChatStyle = this.cameraOptions.getBoolean("isWeChatStyle");
232234
boolean showSelectedIndex = this.cameraOptions.getBoolean("showSelectedIndex");
235+
boolean compressFocusAlpha = this.cameraOptions.getBoolean("compressFocusAlpha");
233236

234237
Boolean isAndroidQ = SdkVersionUtils.checkedAndroid_Q();
235238

@@ -255,6 +258,7 @@ private void openCamera() {
255258
.scaleEnabled(scaleEnabled)// 裁剪是否可放大缩小图片 true or false
256259
.isWeChatStyle(isWeChatStyle)
257260
.theme(showSelectedIndex ? R.style.picture_WeChat_style : 0)
261+
.compressFocusAlpha(compressFocusAlpha)
258262
.forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
259263
}
260264

@@ -396,7 +400,6 @@ private WritableMap getImageResult(LocalMedia media, Boolean enableBase64) {
396400
if (media.isCut()) {
397401
path = media.getCutPath();
398402
}
399-
400403
BitmapFactory.Options options = new BitmapFactory.Options();
401404
options.inJustDecodeBounds = true;
402405
BitmapFactory.decodeFile(path, options);
@@ -442,6 +445,9 @@ private String getBase64StringFromFile(String absoluteFilePath) {
442445
e.printStackTrace();
443446
}
444447
bytes = output.toByteArray();
448+
if(absoluteFilePath.toLowerCase().endsWith("png")){
449+
return "data:image/png;base64," + Base64.encodeToString(bytes, Base64.NO_WRAP);
450+
}
445451
return "data:image/jpeg;base64," + Base64.encodeToString(bytes, Base64.NO_WRAP);
446452
}
447453

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export interface ImagePickerOption {
1515
rotateEnabled: boolean, // 裁剪是否可旋转图片
1616
scaleEnabled: boolean, // 裁剪是否可放大缩小图片
1717
compress: boolean,
18+
compressFocusAlpha:boolean, //压缩png保留通明度
1819
minimumCompressSize: number, // 小于100kb的图片不压缩
1920
quality: number, // 压缩质量
2021
enableBase64: boolean, // 是否返回base64编码,默认不返回

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const defaultOptions = {
2525
rotateEnabled: true, // 裁剪是否可旋转图片
2626
scaleEnabled: true, // 裁剪是否可放大缩小图片
2727
compress: true,
28+
compressFocusAlpha:false, //压缩png保留通明度
2829
minimumCompressSize: 100, // 小于100kb的图片不压缩
2930
quality: 90, // 压缩质量
3031
enableBase64: false, // 是否返回base64编码,默认不返回

ios/RNSyanImagePicker.m

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ - (void)dealloc {
4747

4848
RCT_EXPORT_METHOD(showImagePicker:(NSDictionary *)options
4949
callback:(RCTResponseSenderBlock)callback) {
50-
self.cameraOptions = options;
50+
self.cameraOptions = options;
5151
self.callback = callback;
5252
self.resolveBlock = nil;
5353
self.rejectBlock = nil;
@@ -58,7 +58,7 @@ - (void)dealloc {
5858
options:(NSDictionary *)options
5959
showImagePickerResolver:(RCTPromiseResolveBlock)resolve
6060
rejecter:(RCTPromiseRejectBlock)reject) {
61-
self.cameraOptions = options;
61+
self.cameraOptions = options;
6262
self.resolveBlock = resolve;
6363
self.rejectBlock = reject;
6464
self.callback = nil;
@@ -452,14 +452,15 @@ - (NSDictionary *)handleCropImage:(UIImage *)image phAsset:(PHAsset *)phAsset qu
452452
NSString *fileExtension = [filename pathExtension];
453453
NSMutableString *filePath = [NSMutableString string];
454454
BOOL isPNG = [fileExtension hasSuffix:@"PNG"] || [fileExtension hasSuffix:@"png"];
455-
455+
BOOL compressFocusAlpha = [self.cameraOptions sy_boolForKey:@"compressFocusAlpha"];
456+
456457
if (isPNG) {
457458
[filePath appendString:[NSString stringWithFormat:@"%@SyanImageCaches/%@", NSTemporaryDirectory(), filename]];
458459
} else {
459460
[filePath appendString:[NSString stringWithFormat:@"%@SyanImageCaches/%@.jpg", NSTemporaryDirectory(), [filename stringByDeletingPathExtension]]];
460461
}
461-
462-
NSData *writeData = isPNG ? UIImagePNGRepresentation(image) : UIImageJPEGRepresentation(image, quality/100);
462+
//UIImagePNGRepresentation压缩压缩率太低了可以使用 pngquant
463+
NSData *writeData = (isPNG && compressFocusAlpha) ? UIImagePNGRepresentation(image) : UIImageJPEGRepresentation(image, quality/100);
463464
[writeData writeToFile:filePath atomically:YES];
464465

465466
photo[@"uri"] = filePath;
@@ -469,7 +470,11 @@ - (NSDictionary *)handleCropImage:(UIImage *)image phAsset:(PHAsset *)phAsset qu
469470
photo[@"size"] = @(size);
470471
photo[@"mediaType"] = @(phAsset.mediaType);
471472
if ([self.cameraOptions sy_boolForKey:@"enableBase64"]) {
472-
photo[@"base64"] = [NSString stringWithFormat:@"data:image/jpeg;base64,%@", [writeData base64EncodedStringWithOptions:0]];
473+
if(isPNG){
474+
photo[@"base64"] = [NSString stringWithFormat:@"data:image/png;base64,%@", [writeData base64EncodedStringWithOptions:0]];
475+
}else{
476+
photo[@"base64"] = [NSString stringWithFormat:@"data:image/jpeg;base64,%@", [writeData base64EncodedStringWithOptions:0]];
477+
}
473478
}
474479

475480
return photo;
@@ -485,15 +490,15 @@ - (NSDictionary *)handleOriginalPhotoData:(NSData *)data phAsset:(PHAsset *)phAs
485490
UIImage *image = nil;
486491
NSData *writeData = nil;
487492
NSMutableString *filePath = [NSMutableString string];
488-
489493
BOOL isPNG = [fileExtension hasSuffix:@"PNG"] || [fileExtension hasSuffix:@"png"];
490-
494+
BOOL compressFocusAlpha = [self.cameraOptions sy_boolForKey:@"compressFocusAlpha"];
495+
491496
if (isGIF) {
492497
image = [UIImage sd_tz_animatedGIFWithData:data];
493498
writeData = data;
494499
} else {
495500
image = [UIImage imageWithData: data];
496-
writeData = isPNG ? UIImagePNGRepresentation(image) : UIImageJPEGRepresentation(image, quality/100);
501+
writeData = (isPNG && compressFocusAlpha) ? UIImagePNGRepresentation(image) : UIImageJPEGRepresentation(image, quality/100);
497502
}
498503

499504
if (isPNG || isGIF) {
@@ -511,7 +516,11 @@ - (NSDictionary *)handleOriginalPhotoData:(NSData *)data phAsset:(PHAsset *)phAs
511516
photo[@"size"] = @(size);
512517
photo[@"mediaType"] = @(phAsset.mediaType);
513518
if ([self.cameraOptions sy_boolForKey:@"enableBase64"] && !isGIF) {
514-
photo[@"base64"] = [NSString stringWithFormat:@"data:image/jpeg;base64,%@", [writeData base64EncodedStringWithOptions:0]];
519+
if(isPNG){
520+
photo[@"base64"] = [NSString stringWithFormat:@"data:image/png;base64,%@", [writeData base64EncodedStringWithOptions:0]];
521+
}else{
522+
photo[@"base64"] = [NSString stringWithFormat:@"data:image/jpeg;base64,%@", [writeData base64EncodedStringWithOptions:0]];
523+
}
515524
}
516525

517526
return photo;

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
{
33
"name": "react-native-syan-image-picker",
4-
"version": "0.4.10",
4+
"version": "0.4.11",
55
"description": "React-Native 多图片选择 支持裁剪 压缩",
66
"main": "index.js",
77
"types": "index.d.ts",

0 commit comments

Comments
 (0)