I found that this a known issue in Cordova ( at least, I saw it in their public JIRA account as a bug report ). But nothing seems to be getting done about it.
So, I found a workaround. I ended up not using Ionic’s cropping tool at all. Instead, I used a plugin called Cropperjs ( https://fengyuanchen.github.io/cropperjs/ ). I got a lot of the information on how to implement it from this thread ( Image cropping plugin ). I pulled my implementation together from a few of the posts in there.
First, I created a modal called CropImageModal and I just launch that modal once I get the uncropped image from the camera.
Here is the code for the modal window:
import Cropper from ‘cropperjs’;
import { NavParams, ViewController } from ‘ionic-angular’;
import { Component, ViewChild, ElementRef } from ‘@angular/core’;
@Component({
selector: ‘crop-image-modal’,
template: <div padding text-center> <div><img [src]="imageToCrop" (load)="imageLoaded()" #imageSrc ></div> </div> <p text-center> <button ion-button border round outline (click)="cancel()">CANCEL</button> <button ion-button border round outline (click)="crop()">CROP</button> </p>
})
export class CropImageModal {
@ViewChild(‘imageSrc’) imageInput: ElementRef;
public imageToCrop: any;
public cropper: Cropper;
constructor(public params: NavParams, public viewCtrl: ViewController) {
this.imageToCrop = params.get('imageToCrop');
}
imageLoaded() {
console.log("starting Cropper... ");
this.cropper = new Cropper(this.imageInput.nativeElement, {
aspectRatio: 1 / 1,
dragMode: 'move',
modal: true,
guides: true,
highlight: false,
background: true,
autoCrop: true,
autoCropArea: 0.9,
responsive: true,
crop: (e:Cropper.CropperCustomEvent) => {}
});
}
cancel() {
this.viewCtrl.dismiss(undefined);
}
crop() {
let croppedImage:string = this.cropper.getCroppedCanvas({
width: 500,
height: 500
}).toDataURL('image/jpeg', (100 / 100)); // 100 / 100 = photo quality
this.viewCtrl.dismiss(
{
image: croppedImage
}
);
}
}
And here is all of the relevant code from my page file to launch the cropping modal:
//Import cropper modal
import {CropImageModal} from “…/crop-image/crop-image-modal”;
/**
-
Launches native device’s camera.
*/
takePicture( ){
if( this.platform.is(‘cordova’) ){
let cameraOptions = {
destinationType: Camera.DestinationType.FILE_URI,
encodingType: 0,
quality: 100,
targetWidth: 500,
targetHeight: 500,
correctOrientation: true,
sourceType: Camera.PictureSourceType.SAVEDPHOTOALBUM,
allowEdit: false
}
Camera.getPicture(cameraOptions).then((imageData) => {
// imageData is the URI for the file
this.imageToCrop = imageData;
this.launchCropModal();
}, (err) => {
console.log( 'Error creating image: ', err);
});
}
}
/**
-
Displays a modal window for the user to crop their selected image.
*/
launchCropModal(){
let params = {
imageToCrop: this.imageToCrop
};
let modal = this.modalCtrl.create(CropImageModal, params);
modal.onDidDismiss(data => {
if (data) {
this.childService.child.avatar = this.avatar = data.image;
this.uploadAvatar();
}
});
modal.present();
}