Skip to content

Instantly share code, notes, and snippets.

@vaibhavgehani
Created July 29, 2020 07:08
Show Gist options
  • Select an option

  • Save vaibhavgehani/929652858956159202995ca51129e690 to your computer and use it in GitHub Desktop.

Select an option

Save vaibhavgehani/929652858956159202995ca51129e690 to your computer and use it in GitHub Desktop.
//home.page.ts
import { Component } from '@angular/core';
import { Camera, CameraOptions } from '@ionic-native/Camera/ngx';
import { Crop } from '@ionic-native/crop/ngx';
import { File } from '@ionic-native/file/ngx';
import { ActionSheetController } from '@ionic/angular';
@Component({
selector: 'app-home',
templateUrl: 'home.page.html',
styleUrls: ['home.page.scss'],
})
export class HomePage {
croppedImagepath = "";
isLoading = false;
imagePickerOptions = {
maximumImagesCount: 1,
quality: 50
};
constructor(
private camera: Camera,
private crop: Crop,
public actionSheetController: ActionSheetController,
private file: File
) { }
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
});
}
async selectImage() {
const actionSheet = await this.actionSheetController.create({
header: "Select Image source",
buttons: [{
text: 'Load from Library',
handler: () => {
this.pickImage(this.camera.PictureSourceType.PHOTOLIBRARY);
}
},
{
text: 'Use Camera',
handler: () => {
this.pickImage(this.camera.PictureSourceType.CAMERA);
}
},
{
text: 'Cancel',
role: 'cancel'
}
]
});
await actionSheet.present();
}
cropImage(fileUrl) {
this.crop.crop(fileUrl, { quality: 50 })
.then(
newPath => {
this.showCroppedImage(newPath.split('?')[0])
},
error => {
alert('Error cropping image' + error);
}
);
}
showCroppedImage(ImagePath) {
this.isLoading = true;
var copyPath = ImagePath;
var splitPath = copyPath.split('/');
var imageName = splitPath[splitPath.length - 1];
var filePath = ImagePath.split(imageName)[0];
this.file.readAsDataURL(filePath, imageName).then(base64 => {
this.croppedImagepath = base64;
this.isLoading = false;
}, error => {
alert('Error in showing image' + error);
this.isLoading = false;
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment