Skip to content

Instantly share code, notes, and snippets.

@iamjoyce
Forked from aaronksaunders/snippet.js
Created July 18, 2017 05:52
Show Gist options
  • Save iamjoyce/620c897da7f0bc0ce668f3bcbe22c47a to your computer and use it in GitHub Desktop.
Save iamjoyce/620c897da7f0bc0ce668f3bcbe22c47a to your computer and use it in GitHub Desktop.
Cordova camera - Choose camera OR photo library
//
// you need the actionsheet plugin, image picker plugin and the camera plugin for this code to work
//
/**
* displays an action sheet for the user to select a photo from
* the gallery or using the camera
*
* @param _event {Object} information from the webview on the event
*
*/
function selectPhoto(_event) {
if (_event.keyCode == 13 || _event.x == 0) {
//event.preventDefault();
return true;
}
var options = {
'buttonLabels': ['Take Picture', 'Select From Gallery'],
'addCancelButtonWithLabel': 'Cancel'
};
window.plugins.actionsheet.show(options, function (_btnIndex) {
if (_btnIndex === 1) {
doGetProfilePhoto();
} else if (_btnIndex === 2) {
doGetGalleryPhoto();
}
});
}
/**
* displays the photo gallery for the user to select an image to
* work with
*/
function doGetGalleryPhoto() {
CameraService.getPicturesFromGallery().then(function (imageURI) {
console.log(imageURI);
vm.lastPhoto = imageURI;
vm.newPhoto = true;
}, function (err) {
console.log(err);
vm.newPhoto = false;
alert("Buddy Connector", "Error Getting Photo " + err);
});
}
/**
* displays the camera for the user to select/take a photo
*/
function doGetProfilePhoto() {
var picOptions = {
destinationType: navigator.camera.DestinationType.FILE_URI,
quality: 75,
targetWidth: 500,
targetHeight: 500,
allowEdit: true,
saveToPhotoAlbum: false
};
CameraService.getPicture(picOptions).then(function (imageURI) {
console.log(imageURI);
vm.lastPhoto = imageURI;
vm.newPhoto = true;
}, function (err) {
console.log(err);
vm.newPhoto = false;
alert("Buddy Connector", "Error Getting Photo " + err);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment