How can I modify the size of an image on iOS before upload to server?
For android I used the “targetWidth”-Option of getPicture-Function in ionic-native/camera. Unfortunately it does not work on iOS. Cordova documentation: “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.”
import { Camera } from '@ionic-native/camera';
....
public takePicture(sourceType, task) {
// Create options for the Camera Dialog
var options = {
quality: 50,
targetWidth: 150,
sourceType: sourceType,
saveToPhotoAlbum: false,
correctOrientation: true
};
// Get the data of an image
this.camera.getPicture(options).then((imagePath) => {
// Special handling for Android library
if (this.platform.is('android') && sourceType === this.camera.PictureSourceType.PHOTOLIBRARY) {
this.filePath.resolveNativePath(imagePath)
.then(filePath => {
let correctPath = filePath.substr(0, filePath.lastIndexOf('/') + 1);
let currentName = imagePath.substring(imagePath.lastIndexOf('/') + 1, imagePath.lastIndexOf('?'));
this.copyFileToLocalDir(correctPath, currentName, this.createFileName())
.then(newFileName => {
if (newFileName == 'Error while storing file.') {
this.loading.dismissAll();
this.presentToast('Error while uploading file.');
} else {
this.uploadImage(task)
}
});
});
} else {
var currentName = imagePath.substr(imagePath.lastIndexOf('/') + 1);
var correctPath = imagePath.substr(0, imagePath.lastIndexOf('/') + 1);
this.copyFileToLocalDir(correctPath, currentName, this.createFileName()).then(newFileName => {
if (newFileName == 'Error while storing file.') {
this.loading.dismissAll();
this.presentToast('Error while uploading file.');
} else {
this.uploadImage(task)
}
});
}
}, (err) => {
this.presentToast('Error while selecting image.');
});
}