Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
R
react-native-syan-image-picker
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
JIRA
JIRA
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
OpenSource
react-native-syan-image-picker
Commits
8796a035
Commit
8796a035
authored
Mar 30, 2018
by
cookiej
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
支持是否返回base64设置 & 返回图片大小
parent
f1369e32
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
62 additions
and
49 deletions
+62
-49
RNSyanImagePickerModule.java
...c/main/java/com/reactlibrary/RNSyanImagePickerModule.java
+39
-31
index.js
index.js
+2
-1
RNSyanImagePicker.m
ios/RNSyanImagePicker.m
+21
-17
No files found.
android/src/main/java/com/reactlibrary/RNSyanImagePickerModule.java
View file @
8796a035
...
@@ -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
=
o
ptions
.
getInt
(
"imageCount"
);
int
imageCount
=
this
.
cameraO
ptions
.
getInt
(
"imageCount"
);
boolean
isCamera
=
o
ptions
.
getBoolean
(
"isCamera"
);
boolean
isCamera
=
this
.
cameraO
ptions
.
getBoolean
(
"isCamera"
);
boolean
isCrop
=
o
ptions
.
getBoolean
(
"isCrop"
);
boolean
isCrop
=
this
.
cameraO
ptions
.
getBoolean
(
"isCrop"
);
int
CropW
=
o
ptions
.
getInt
(
"CropW"
);
int
CropW
=
this
.
cameraO
ptions
.
getInt
(
"CropW"
);
int
CropH
=
o
ptions
.
getInt
(
"CropH"
);
int
CropH
=
this
.
cameraO
ptions
.
getInt
(
"CropH"
);
boolean
isGif
=
o
ptions
.
getBoolean
(
"isGif"
);
boolean
isGif
=
this
.
cameraO
ptions
.
getBoolean
(
"isGif"
);
boolean
showCropCircle
=
o
ptions
.
getBoolean
(
"showCropCircle"
);
boolean
showCropCircle
=
this
.
cameraO
ptions
.
getBoolean
(
"showCropCircle"
);
boolean
showCropFrame
=
o
ptions
.
getBoolean
(
"showCropFrame"
);
boolean
showCropFrame
=
this
.
cameraO
ptions
.
getBoolean
(
"showCropFrame"
);
boolean
showCropGrid
=
o
ptions
.
getBoolean
(
"showCropGrid"
);
boolean
showCropGrid
=
this
.
cameraO
ptions
.
getBoolean
(
"showCropGrid"
);
int
quality
=
o
ptions
.
getInt
(
"quality"
);
int
quality
=
this
.
cameraO
ptions
.
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
=
o
ptions
.
getBoolean
(
"isCrop"
);
boolean
isCrop
=
this
.
cameraO
ptions
.
getBoolean
(
"isCrop"
);
int
CropW
=
o
ptions
.
getInt
(
"CropW"
);
int
CropW
=
this
.
cameraO
ptions
.
getInt
(
"CropW"
);
int
CropH
=
o
ptions
.
getInt
(
"CropH"
);
int
CropH
=
this
.
cameraO
ptions
.
getInt
(
"CropH"
);
boolean
showCropCircle
=
o
ptions
.
getBoolean
(
"showCropCircle"
);
boolean
showCropCircle
=
this
.
cameraO
ptions
.
getBoolean
(
"showCropCircle"
);
boolean
showCropFrame
=
o
ptions
.
getBoolean
(
"showCropFrame"
);
boolean
showCropFrame
=
this
.
cameraO
ptions
.
getBoolean
(
"showCropFrame"
);
boolean
showCropGrid
=
o
ptions
.
getBoolean
(
"showCropGrid"
);
boolean
showCropGrid
=
this
.
cameraO
ptions
.
getBoolean
(
"showCropGrid"
);
int
quality
=
o
ptions
.
getInt
(
"quality"
);
int
quality
=
this
.
cameraO
ptions
.
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
if
(
cameraOptions
.
getBoolean
(
"enableBase64"
))
{
byte
[]
encode
=
Base64
.
encode
(
bytes
,
Base64
.
DEFAULT
);
byte
[]
encode
=
Base64
.
encode
(
bytes
,
Base64
.
DEFAULT
);
String
encodeString
=
new
String
(
encode
);
String
encodeString
=
new
String
(
encode
);
aImage
.
putString
(
"base64"
,
encodeString
);
aImage
.
putString
(
"base64"
,
encodeString
);
}
}
else
{
}
else
{
// 压缩过,取 media.getCompressPath();
// 压缩过,取 media.getCompressPath();
BitmapFactory
.
decodeFile
(
media
.
getCompressPath
(),
options
);
BitmapFactory
.
decodeFile
(
media
.
getCompressPath
(),
options
);
...
@@ -224,16 +228,20 @@ public class RNSyanImagePickerModule extends ReactContextBaseJavaModule {
...
@@ -224,16 +228,20 @@ 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
if
(
cameraOptions
.
getBoolean
(
"enableBase64"
))
{
byte
[]
encode
=
Base64
.
encode
(
bytes
,
Base64
.
DEFAULT
);
byte
[]
encode
=
Base64
.
encode
(
bytes
,
Base64
.
DEFAULT
);
String
encodeString
=
new
String
(
encode
);
String
encodeString
=
new
String
(
encode
);
aImage
.
putString
(
"base64"
,
encodeString
);
aImage
.
putString
(
"base64"
,
encodeString
);
}
}
}
if
(
media
.
isCut
())
{
if
(
media
.
isCut
())
{
aImage
.
putString
(
"original_uri"
,
"file://"
+
media
.
getCutPath
());
aImage
.
putString
(
"original_uri"
,
"file://"
+
media
.
getCutPath
());
}
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
);
}
}
...
...
index.js
View file @
8796a035
...
@@ -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
{
...
...
ios/RNSyanImagePicker.m
View file @
8796a035
...
@@ -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
openImagePicker
WithOptions
:
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
openImagePicker
WithOptions
:
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
)
openImagePicker
WithOptions
:
(
NSDictionary
*
)
options
{
-
(
void
)
openImagePicker
{
// 照片最大可选张数
// 照片最大可选张数
NSInteger
imageCount
=
[
o
ptions
sy_integerForKey
:
@"imageCount"
];
NSInteger
imageCount
=
[
self
.
cameraO
ptions
sy_integerForKey
:
@"imageCount"
];
// 显示内部拍照按钮
// 显示内部拍照按钮
BOOL
isCamera
=
[
o
ptions
sy_boolForKey
:
@"isCamera"
];
BOOL
isCamera
=
[
self
.
cameraO
ptions
sy_boolForKey
:
@"isCamera"
];
BOOL
isCrop
=
[
o
ptions
sy_boolForKey
:
@"isCrop"
];
BOOL
isCrop
=
[
self
.
cameraO
ptions
sy_boolForKey
:
@"isCrop"
];
BOOL
isGif
=
[
o
ptions
sy_boolForKey
:
@"isGif"
];
BOOL
isGif
=
[
self
.
cameraO
ptions
sy_boolForKey
:
@"isGif"
];
BOOL
showCropCircle
=
[
o
ptions
sy_boolForKey
:
@"showCropCircle"
];
BOOL
showCropCircle
=
[
self
.
cameraO
ptions
sy_boolForKey
:
@"showCropCircle"
];
NSInteger
CropW
=
[
o
ptions
sy_integerForKey
:
@"CropW"
];
NSInteger
CropW
=
[
self
.
cameraO
ptions
sy_integerForKey
:
@"CropW"
];
NSInteger
CropH
=
[
o
ptions
sy_integerForKey
:
@"CropH"
];
NSInteger
CropH
=
[
self
.
cameraO
ptions
sy_integerForKey
:
@"CropH"
];
NSInteger
circleCropRadius
=
[
o
ptions
sy_integerForKey
:
@"circleCropRadius"
];
NSInteger
circleCropRadius
=
[
self
.
cameraO
ptions
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
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment