Skip to content

Instantly share code, notes, and snippets.

@boydlee
Created August 20, 2012 12:38
Show Gist options
  • Save boydlee/3403722 to your computer and use it in GitHub Desktop.
Save boydlee/3403722 to your computer and use it in GitHub Desktop.
CommonJS module to access photo gallery and camera and return resulting image
/*
PhotoSelector.js
Gets a photo from camera or gallery
Usage:
var ps = require('PhotoSelector');
var select = new ps.PhotoSelector();
//get a photo from the camera
select.fromCamera(false, true, function(success){}, function(failed){}, function(cancelled){});
//or get a photo from the gallery
select.fromGallery(function(success){}, function(failed){}, function(cancelled){});
*/
function PhotoSelector(){
};
/*
*
*/
PhotoSelector.prototype.fromCamera = function(saveToGallery, allowEditing, success, failure, cancel) {
Titanium.Media.showCamera({
success:function(event)
{
Ti.API.debug('Our type was: '+ event.mediaType);
if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
{
success(event.media);
}
},
cancel: cancel,
error:function(err)
{
failure(err);
},
saveToPhotoGallery: saveToGallery,
allowEditing: allowEditing,
mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
});
};
/*
*
*/
PhotoSelector.prototype.fromGallery = function(success, failure, cancel) {
Titanium.Media.openPhotoGallery({
success:function(event)
{
Ti.API.debug('Our type was: '+event.mediaType);
if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
{
success(event.media);
}
},
cancel: cancel,
error:function(err)
{
failure(err);
},
mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
});
};
//finally,
exports.PhotoSelector = PhotoSelector;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment