Preview 2.0 is now in Public Beta!
Read the Announcement

@nativescript/imagepicker

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.

Table of Contents

Installation

npm install @nativescript/imagepicker

Required Permissions

Android

Add the following permissions to the App_Resources/Android/src/main/AndroidManifest.xml file:

  • targetSdkVersion < 33
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

<application android:requestLegacyExternalStorage="true" ... >
    ...
  </application>
  • targetSdkVersion >=33(Android 13+)
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />

<uses-permission android:name="android.permission.READ_MEDIA_VIDEO" />

See the complete example here.

iOS

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.

Usage

You can play with the plugin on StackBlitz at the following links:

Importing the plugin

import * as imagePickerPlugin from '@nativescript/imagepicker'
var imagePickerPlugin = require('@nativescript/imagepicker')

Creating the Imagepicker instance

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' })

Using the ImagePicker Instance

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.

API

ImagePicker

MethodReturnsDescription
constructor(options: Options)ImagePickerInstanciates 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.

create()

imagePicker: ImagePicker = imagePickerPlugin.create(options: Options, hostView: View)

Creates an instance of the ImagePicker class.

  • Optional: options - The ImagePicker instance settings. See Options
  • Optional: (iOS-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.

Options

OptionTypeDefaultDescription
modestringmultipleThe mode of the imagepicker. Possible values are single for single selection and multiple for multiple selection.
minimumNumberOfSelectionnumber0Optional: (iOS-only) The minumum number of selected assets.
maximumNumberOfSelectionnumber0Optional: (iOS-only) The maximum number of selected assets.
showsNumberOfSelectedAssetsbooleantrueOptional: (iOS-only) Display the number of selected assets.
promptstringundefinedOptional: (iOS-only) Display prompt text when selecting assets.
numberOfColumnsInPortraitnumber4Optional: (iOS-only) Sets the number of columns in Portrait orientation
numberOfColumnsInLandscapenumber7Optional: (iOS-only) Sets the number of columns in Landscape orientation.
mediaTypeImagePickerMediaTypeAnyOptional: The type of media asset to pick whether to pick Image/Video/Any type of assets.
showAdvanced booleanfalseOptional😦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.

ImagePickerMediaType

  • Any = 0,
  • Image = 1,
  • Video = 2

License

Apache License Version 2.0