enappd loader


Image picker in IONIC 4 app


Summary

This is Part-1 of two post series. In this post, you will learn how to implement an Image picker in Ionic 4 app. Part 2 of the series discusses how to Crop those images in IONIC 4 app🔥

We will create a sample app, where users can pick an image from there gallery or camera. After picking, the user can crop his image.

Complete source code of this tutorial is available in the Image picker and croper in IONIC 4 app

What is Ionic 4?

You probably already know about Ionic, but put this section in every blog just for the sake of beginners. Ionic is a complete open-source SDK for hybrid mobile app development. Ionic provides tools and services for developing hybrid mobile apps using Web technologies like CSS, HTML5, and Sass. Apps can be built with these Web technologies and then distributed through native app stores to be installed on devices.

In other words — If you create native apps in Android, you code in Java. If you create native apps in iOS, you code in Obj-C or Swift. Both of these are powerful, but complex languages. With Cordova (and Ionic) you can write a single piece of code for your app that can run on both iOS and Android (and windows!), that too with the simplicity of HTML, CSS, and JS. I’m a huge fan of Ionic and been developing Ionic apps for last 4 years.

Structure

We’ll follow a stepped approach to create a Music player app in Ionic 4. We’ll use an IONIC native plugin for music playback. Following are the steps

  • Step 1 — Create a basic Ionic 4 app
  • Step 2 — Setup Image Picker plugin
  • Step 3— Use Image Picker Plugin In App
  • Step 4— Various Image Picker Features

So let’s dive right in!

Step 1 — Create a basic Ionic 4 app

I have covered this topic in detail in this blog.

In short, the steps you need to take here are

  • Make sure you have node installed in the system (V10.15.3 at the time of this blog post)
  • Install ionic CLI using npm (my Ionic version is 4.12.0 currently)
  • Create an Ionic app using ionic start

You can create a sidemenu starter for the sake of this tutorial. On running ionic start ionic-4-chartjs sidemenu , node modules will be installed. Once the installation is done, run your app on browser using

$ ionic serve

The app will launch on the browser. You can go to Inspect -> Device Mode to see the code in a mobile layout. Next, we’ll add the chart library to our app.

Step 2 — Setup Image Picker plugin

Requires the Cordova plugin: cordova-plugin-camera. For more info, please see the Cordova Camera Plugin Docs.

[Warning] Since IOS 10 the camera requires permissions to be placed in your config.xml add

<config-file parent="NSCameraUsageDescription" platform="ios" target="*-Info.plist">
<string>You can take photos</string>
</config-file>

inside of the <platform name=’ios> section

Installation

‌For that, open your terminal and type

ionic cordova plugin add cordova-plugin-camera

‌It’s a bit clumsy to work with Cordova plugin so the ionic team created Ionic Native, which is a wrapper for the Cordova plugins so we can use them in a more “Angular/Ionic” way.

‌So now we will open our terminal and try this command‌ to install Facebook package from Ionic Native

npm install @ionic-native/camera

For read/write access to files residing on the device. you also need File plugin. The File class implements static convenience functions to access files and directories.

so for installation of this plugin open your terminal and type,

ionic cordova plugin add cordova-plugin-file

and

npm install @ionic-native/file

Step 3 — Use Image Picker Plugin In App

Using this plugin The first step you will need to do is add this plugin to your app’s module

Import these plugins like this

import { Camera } from '@ionic-native/Camera/ngx';
import { File } from '@ionic-native/file/ngx';

and add this to providers of your app Like this

providers: [
StatusBar,
File,
SplashScreen,
Camera,
File,
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],

So after Adding Your app.module.ts look like this


Now time to import this plugin in your home.ts where we will use this plugin

So for using this plugin in our home.ts first, we will import the plugin like this

import { Camera, CameraOptions } from '@ionic-native/Camera/ngx';

and inject it in your Constructor (Dependency injection) like this

constructor(
private camera: Camera,
public actionSheetController: ActionSheetController,
private file: File
) { }

And use this code for Adding Image Picker in IONIC App

pickImage(sourceType) {
const options: CameraOptions = {
quality: 100,
sourceType: sourceType,
destinationType: this.camera.DestinationType.FILE_URI,
encodingType: this.camera.EncodingType.JPEG,
mediaType: this.camera.MediaType.PICTURE
}
this.camera.getPicture(options).then((imageData) => {
// imageData is either a base64 encoded string or a file URI
// If it's base64 (DATA_URL):
// let base64Image = 'data:image/jpeg;base64,' + imageData;
this.cropImage(imageData)
}, (err) => {
// Handle error
});
}

So after Adding this code your home.ts something looks like this.






Step 4 — Various Image Picker Features

Camera.DestinationType : enum

Defines the output format of Camera.getPicture call. Note: On iOS passing DestinationType.NATIVE_URI along withPictureSourceType.PHOTOLIBRARY or PictureSourceType.SAVEDPHOTOALBUM will disable any image modifications (resize, quality change, cropping, etc.) due to implementation-specific.

Kind: static enum property of Camera

Properties


Camera.EncodingType : enum

Kind: static enum property of Camera
Properties


Camera.MediaType : enum

Kind: static enum property of Camera
Properties


Camera.PictureSourceType : enum

Defines the output format of Camera.getPicture call. Note: On iOS passing PictureSourceType.PHOTOLIBRARY or PictureSourceType.SAVEDPHOTOALBUM along with DestinationType.NATIVE_URI will disable any image modifications (resize, quality change, cropping, etc.) due to implementation-specific.

Kind: static enum property of Camera
Properties







Conclusion

In this post, you learned how to implement Image Picker in your Ionic 4 app. In the next part of this post, you’ll learn how to crop Image in IONIC 4 app.

Complete source code of this tutorial is available in the ionic-4-image-picker-cropper

Next Steps

Now that you have learnt how to implement Twitter login in Ionic 4 apps, there are several things you can do next

Need FREE Ionic 4 Starters ?

You can also find free Ionic 4 starters on our website enappd.com

You can also make your next awesome app using Ionic 4 Full App



Make your next awesome app with Ionic 4 Full App



Title
Subtitle
Kicker