Imagepicker plugin supporting both single and multiple selection. As of 2.0, it returns rich information about your selection including filesize, path, video duration and thumbnails etc.
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.
Note: Version 2.0 contains breaking changes. In order supply more information about your selection, the ImageSource asset is nested in the response so you'll need to update your code to use result.asset
instead of result
as your src for your Images.
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) {
this.imageSource = selected.asset
this.type = selected.type
this.filesize = selected.filesize
//etc
})
list.items = selection
})
.catch(function (e) {
// process error
})
Note
To request permissions for Android 6+ (API 23+), use @nativescript-community/perms 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.As of version 2.0, imagepicker returns more information about your selection.
;[
{
asset: {}, // ImageSource. this is what you'll use to set the src of an Image for example.
type: 'image', // either 'video' or 'image'
filename: 'mycoolfile-0.jpg', // the filename
originalFilename: 'IMG1001.JPG', // the original filename (may be useful if you changed the filename when copying)
path: '/myapp/media/mycoolfile-0.jpg', // the path where the file is
duration: 100, // video only, the duration in seconds.
thumbnail: {} // ImageSource, video only. This is what you'll use to set the src of an Image for example.
}
]
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. |
copyToAppFolder | string | undefined | Optional: If passed, a new folder will be created in your applications folder and the asset will be copied there. |
renameFileTo | string | undefined | Optional: If passed, the copied file will be named what you choose. If you select multiple, -index will be appended. |
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