cordova-plugin-ImagePicker
I am very grateful to Nanchen and banchichen for providing source code to support multi-star open source. It is not easy, thank you. KouKou group: 273613165
One supports multiple selections, and the album implements functions such as taking pictures, previewing, and compressing pictures.
Function
- Album catalog
- Multiple choice picture
- Taking photos inside the album
- Preview selected image
- Image compression
Installation requirements
- Cordova Version >= 6.0.0
- Cordova-Android >= 6.0.0
- Cordova-iOS >= 4.0.0
Install
-
cordova plugin add https://github.com/giantss/cordova-plugin-ImagePicker.git
Note: Do not build the Android project directly first, see Android Precautions.
Android video demonstration
- Click to view the video (Youku)
iOS video demo
- Click to view the video (Youku)
renderings
Android | iOS |
---|
| |
Usage
A rough cordova demo
// 选图
ImagePicker . getPictures ( function ( result ) {
alert ( JSON . stringify ( result ) ) ;
} , function ( err ) {
alert ( err ) ;
} , {
maximumImagesCount : 9 ,
width : 1920 ,
height : 1440 ,
quality : 100
} ) ;
// 拍照
ImagePicker . takePhoto ( function ( result ) {
alert ( JSON . stringify ( result ) ) ;
} , function ( err ) {
alert ( err ) ;
} , {
width : 1920 ,
height : 1440 ,
quality : 50
} ) ;
The return result is as follows:
// 如果是拍照,images 数组中只有一个对象
{
"images": [{
"path": "/data/user/0/com.pushsoft.im2/cache/ImagePicker/152783817455118.jpg",
"uri": "file:///data/user/0/com.pushsoft.im2/cache/ImagePicker/152783817455118.jpg",
"width": 720,
"height": 1280,
"size": 104871 // 文件体积(单位:字节)
}, {
"path": "/data/user/0/com.pushsoft.im2/cache/ImagePicker/152783817464525.jpg",
"uri": "file:///data/user/0/com.pushsoft.im2/cache/ImagePicker/152783817464525.jpg",
"width": 720,
"height": 1280,
"size": 109873
}],
"isOrigin": false // 是否原图
}
To use this plug-in in ionic, you need to declare: declare let ImagePicker:any
Parameter meaning
Configuration parameters | Parameter meaning |
---|
maximumImagesCount | Multi-select limit quantity, default is 9 |
width | Set the width of the output image, the default is automatic |
height | Set the height of the output image, the default is automatic |
quality | Image quality defaults to 80 |
enablePickOriginal | Allow selection of original image, default true |
Notice:
- Parameters are optional, if not passed, the default value will be used;
- If width > 0 and height > 0: the compressed image may be larger than the original image or the compression rate is not high (for example, the original image is 4MB, and the compressed image is 2MB), the quality can be set lower, such as 50;
- If width < 0 or height < 0: the compression logic is close to that of WeChat, and the appropriate resolution and compression quality are automatically selected. This method is recommended. The compression library uses Luban and Luban-iOS. If you encounter problems such as unclear compression, please file issues on their project;
- When running, there is an "Original Image" radio button on the image selection interface. After selecting it, the image returned is the uncompressed original image.
android precautions
First remove the installed old plug-ins
If build reports the following error
error: resource android:attr/dialogCornerRadius not found
error: resource android:attr/fontVariationSettings not found
error: resource android:attr/ttcIndex not found
Please use the cordova-android-support-gradle-release plug-in to unify the Android Support library version in the project:
$ cordova plugin add cordova-android-support-gradle-release --variable ANDROID_SUPPORT_VERSION={required version}
The {required version}
value is similar to 26.+
, 27.+
, 28.+
.
If you are using a lower version of Cordova and Gradle, an error will be reported. implementation
Cordova 7.1.0 and below is not supported (corresponding to [email protected] and below). Please change cordova-plugin-ImagePickersrcandroidimagepicker.gradle
implementation
in cordova-plugin-ImagePickersrcandroidimagepicker.gradle
is changed to compile
because the lower version of Cordova-Android uses a lower version of Gradle and does not support implementation
.
If the build still fails
$ cordova platform rm android
or
$ cordova platform rm ios
Remove the old platform and add it again
Reference project
Multiple selection picture items
- jeasonlzy/ImagePicker (Android)
- nanchen2251/ImagePicker
- CysionLiu/ImagePicker
- banchichen/TZImagePickerController (iOS)
Image compression library
- nanchen2251/CompressHelper (Android)
- Curzibn/Luban (Android)
- GuoZhiQiang/Luban_iOS (iOS)
Update instructions
v1.2.2
- (ios) Improvement: iOS 13 crashes when selecting multiple pictures and other issues
v1.2.1
- (Android) Improvement: Glide library 4.10.+ causes conflict between android.support library and androidx
- (Android) Improvement: Set up radio selection first, and then set up multi-selection, but the selector still has a radio-selection problem.
v1.1.9
- (iOS) Add
takePhoto
method to directly enter photo taking
v1.1.8
- (iOS)Luban compression logic fix
v1.1.7
- Updated to the latest third-party image selection library
- Added enablePickOriginal configuration item (allows the original image to be selected, that is, whether to display the original image check box)
- (iOS) supports HEIC image format
v1.1.6
- (Android) Remove picasso reference and related implementation logic code
v1.1.5
- (iOS) Unselect image support callback function
v1.1.4
- The return value increases the image width, height and file size.
- (iOS)Fixed the problem of blurring some images in ios compression
v1.1.3
- (Android) Fixed the problem of diamond operator exception in build when using lower versions of Cordova-Android and Gradle.
- (Android) Add
takePhoto
method to directly enter photo taking - (Android) Add a failure callback, which will be triggered when the image is cancelled.
v1.1.2
- (Android) Fixed the problem that when returning to the picture preview page on some mobile phones, all the small pictures on the picture wall are gone.
v1.1.1
- (Android) Adapted to Cordova@8, Cordova-Android@7
- (Android)provider_paths.xml is renamed, FileProvider is renamed, it will not conflict with other plug-ins, and there is no need to manually copy it to cordova/platform/android/res/xml/
- (Android) Remove styles.xml and set styles in the code to avoid conflicts with other plug-ins that also have files with the same name.
- (Android) Glide picture library has been updated to the latest, supports Gif, and the performance has been greatly improved. There should be no problem that the picture wall cannot display the preview image.
- (Android) Add "Original Image" radio button on the bottom bar of the picture wall
- (iOS) Fixed the problem in iOS that when the original image is checked, the returned image is not the original image, and the compressed image is larger than the original image.
- Parameters such as maximumImagesCount, width, and height are optionally passed
- If width < 0 and height < 0, the appropriate resolution and compression ratio will be automatically selected for compression, which is close to WeChat’s compression logic.
- Modify the return parameter of the success callback. The original image path array
['xxx', 'yyy']
was returned, and now it is { images: ['xxx', 'yyy'], isOrigin: true/false }
License
The MIT License (MIT)