Skip to content

Instantly share code, notes, and snippets.

@thEpisode
Last active September 21, 2017 17:32
Show Gist options
  • Save thEpisode/81a62c56cecdcdac4da89d87eb505aad to your computer and use it in GitHub Desktop.
Save thEpisode/81a62c56cecdcdac4da89d87eb505aad to your computer and use it in GitHub Desktop.
Ionic 3.x camera
<ion-header>
<ion-navbar>
<ion-title>
Taking Photo
</ion-title>
</ion-navbar>
</ion-header>
<ion-content padding>
<img [src]="image" #imageResult />
<ion-fab right bottom>
<button ion-fab (click)="openMenu()" color="danger"><ion-icon name="add"></ion-icon></button>
</ion-fab>
</ion-content>
import { Component, ViewChild, ElementRef, NgZone } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Camera } from '@ionic-native/camera';
import { Platform, ActionSheetController, LoadingController } from 'ionic-angular';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage {
image: string = '';
_zone: any;
constructor(
private camera: Camera,
public navCtrl: NavController,
public platform: Platform,
public loadingCtrl: LoadingController,
public actionsheetCtrl: ActionSheetController) {
this._zone = new NgZone({ enableLongStackTrace: false });
}
/// Execute a menu
openMenu() {
let actionSheet = this.actionsheetCtrl.create({
title: 'Actions',
cssClass: 'action-sheets-basic-page',
buttons: [
{
text: 'Take Photo',
icon: !this.platform.is('ios') ? 'camera' : null,
handler: () => {
this.takePicture()
}
},
{
text: 'Cancel',
role: 'cancel', // will always sort to be on the bottom
icon: !this.platform.is('ios') ? 'close' : null,
handler: () => {
console.log('Cancel clicked');
}
}
]
});
actionSheet.present();
}
takePicture() {
let loader = this.loadingCtrl.create({
content: 'Please wait...'
});
loader.present();
// Take a picture saving in device, as jpg and allows edit
this.camera.getPicture({
quality: 100,
destinationType: this.camera.DestinationType.NATIVE_URI,
encodingType: this.camera.EncodingType.JPEG,
targetHeight: 1000,
sourceType: 1,
allowEdit: true,
saveToPhotoAlbum: true,
correctOrientation: true
}).then((imageURI) => {
loader.dismissAll();
// bind the URI returned by API
this.image = imageURI;
}, (err) => {
console.log(`ERROR -> ${JSON.stringify(err)}`);
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment