Imagepicker plugin supporting both single and multiple selection.
Plugin supports iOS8+ and uses QBImagePicker cocoapod.
For Android it uses Intents to open the stock images or file pickers. For Android 6 (API 23) and above, the permissions to read file storage should be explicitly required.
npm install @nativescript/imagepicker
Add the following permissions to the App_Resources/Android/src/main/AndroidManifest.xml
file:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />
See the complete example here.
Using the plugin on iOS requires the NSPhotoLibraryUsageDescription
permission. Modify the app/App_Resources/iOS/Info.plist
file to add it as follows:
<key>NSPhotoLibraryUsageDescription</key>
<string>Description text goes here</string>
Apple App Store might reject your app if you do not describe why you need this permission. The default message Requires access to photo library.
might not be enough for the App Store reviewers.
You can play with the plugin on StackBlitz at the following links:
import * as imagePickerPlugin from '@nativescript/imagepicker'
var imagePickerPlugin = require('@nativescript/imagepicker')
Create an Imagepicker instance in single
or multiple
mode to specifiy if the image picker will be used for single or multiple selection.
let imagePickerObj: ImagePicker = imagePickerPlugin.create({
mode: 'single'
})
var imagePickerObj = imagePickerPlugin.create({ mode: 'single' })
Once you've created the picker instance, use it to request for permissions, present the list of media assets to be picked from, and process the selection.
imagePickerObj
.authorize()
.then(function () {
return imagePickerObj.present()
})
.then(function (selection) {
selection.forEach(function (selected) {
// process the selected image
})
list.items = selection
})
.catch(function (e) {
// process error
})
Note
To request permissions for Android 6+ (API 23+), use nativescript-permissions plugin.
Method | Returns | Description |
---|---|---|
constructor(options: Options) | ImagePicker | Instanciates the ImagePicker class with the optional options parameter. See Options |
authorize() | Promise<void> | Requests the required permissions. Call it before calling present() . In case of a failed authorization, consider notifying the user for degraded functionality. |
present() | Promise<ImageAsset[]> | Presents the image picker UI. |
imagePicker: ImagePicker = imagePickerPlugin.create(options: Options, hostView: View)
Creates an instance of the ImagePicker class.
options
- The ImagePicker instance settings. See OptionsiOS-only
) hostView
- Can be set to the view that hosts the image picker. Intended to be used when opening the picker from a modal page.Option | Type | Default | Description |
---|---|---|---|
mode | string | multiple | The mode of the imagepicker. Possible values are single for single selection and multiple for multiple selection. |
minimumNumberOfSelection | number | 0 | Optional: (iOS-only ) The minumum number of selected assets. |
maximumNumberOfSelection | number | 0 | Optional: (iOS-only ) The maximum number of selected assets. |
showsNumberOfSelectedAssets | boolean | true | Optional: (iOS-only ) Display the number of selected assets. |
prompt | string | undefined | Optional: (iOS-only ) Display prompt text when selecting assets. |
numberOfColumnsInPortrait | number | 4 | Optional: (iOS-only ) Sets the number of columns in Portrait orientation |
numberOfColumnsInLandscape | number | 7 | Optional: (iOS-only ) Sets the number of columns in Landscape orientation. |
mediaType | ImagePickerMediaType | Any | Optional: The type of media asset to pick whether to pick Image/Video/Any type of assets. |
showAdvanced | boolean | false | Optional😦Android-only ) Show internal and removable storage options on Android (WARNING: not supported officially). |
android | {read_external_storage: string;} | Optional: (Android-only ) Provides a reason for permission request to access external storage on API level above 23. |
Any
= 0
,Image
= 1
,Video
= 2
Apache License Version 2.0