Introduction
Ionic is an open-source framework. It's a very popular cross-platform mobile app framework which is helpful to develop or build hybrid mobile apps easily. It will save a lot of time and money for investment.
This article demonstrates how to select multiple images and load images in the ionic application page using native camera plugin in ionic 3. The following tools and requirements should be prepared before reading this article.
Once we have installed Node.js we can install other tools on the terminal or command line. Run the following command step by step.
npm install -g ionic Cordova
Once the installation is finished let’s start creating an ionic 3 app.
Create Ionic 3 app
Open your Node.js terminal and open a specific location to create ionic 3 apps.
Start a new Ionic project using ionic start command,
ionic start myApp
This command will take a few minutes as it installs all dependencies and modules for a project.
Now, change the location to myApp
cd myApp
Once changed, the location myApp, Add android platform to the app.
- ionic Cordova platform add android
- ionic Cordova platform add a browser (For Browser)
Now, run a project using the following command.
- ionic serve (For Browser)
- ionic Cordova run Android (For Device)
Make sure you have connected the device and settings have been set.
Now let’s start,
The Image Picker Plugin is used to select multiple images.
Step 1
Follow the below-mentioned steps to install the Image Picker plugin to our app.
ionic Cordova plugin add Cordova-plugin-Telerik-imagepicker --variable PHOTO_LIBRARY_USAGE_DESCRIPTION="your usage message"
npm install --save @ionic-native/image-picker
Step 2
Add Camera plugin to your app module.
app.module.ts file
-
- import { ImagePicker } from '@ionic-native/image-picker';
- @NgModule({
- providers: [
-
- ImagePicker
- ]
- })
- export class AppModule { }
Step 3
Integrate image picker with component part in our app.
Sample.ts File
- import {
- ImagePicker,
- ImagePickerOptions
- } from '@ionic-native/image-picker';
-
- public imageLists: any[] = [];
-
- constructor(private imagePicker: ImagePicker) {}
- public loadMultipleImageFromGallery() {
- let options: ImagePickerOptions = {
-
- quality: 100,
-
- width: 600,
-
- height: 600,
-
-
-
-
-
- DATA_URI: 1(number) it returns a base64 data
- for an image
- outputType: 1
-
- maximumImagesCount: 15(1 - 15) numbers
-
- };
- this.imagePicker.getPictures(options).then((results) => {
- for (let index = 0; index < results.length; index++) {
-
- this.imageLists.push('data:image/jpeg;base64,' + results[index]);
- }
- console.log(“Image Lists”, this.imageLists);
- }, (error) => {
-
- console.log(“Error occurred
- while loading”, error);
- });
- }
Step 4
Here is the code to display images in our pages. Here, first I am checking if imageLists is available or not. Next check if imageLists length has been greater than zero(0). Then I use [src] Propety Binding to bind base64 data to img source tag.
- <ng-container *ngIf="imageLists">
- <ion-item>
- <ion-label>Browsed Images</ion-label>
- </ion-item>
- <ion-item>
- <ion-row *ngIf="imageLists.length>0">
- <ion-col style="padding:10px;" col-3 *ngFor="let image of imageLists;">
- <ion-card>
- <ion-thumbnail item-start>
- <img [src]="image" alt="Sample Image">
- </ion-thumbnail>
- </ion-card>
- </ion-col>
- </ion-row>
- </ion-item>
- </ng-container>
In this article, I have discussed how to select multiple images and load images in our ionic application page using native camera plugin in ionic 3. If you have any questions/issues about this article, please let me know in the comments.