Skip to content

Instantly share code, notes, and snippets.

@benbahrenburg
Created July 24, 2011 23:57
Show Gist options
  • Save benbahrenburg/1103263 to your computer and use it in GitHub Desktop.
Save benbahrenburg/1103263 to your computer and use it in GitHub Desktop.
Camera Callback
// this sets the background color of the master UIView (when there are no windows/tab groups on it)
Ti.UI.setBackgroundColor('#000');
Ti.UI.iPhone.statusBarStyle = Ti.UI.iPhone.StatusBar.OPAQUE_BLACK;
//Create main app namespace
var demo={};
//Create a few helpers
demo.myHelpers = {
isAndroid : function(){
return (Ti.Platform.name == 'android');
},
makeWindow : function(a){
a = a || {};
var win = Ti.UI.createWindow(a);
//Force the orientations
win.orientationModes = [
Ti.UI.PORTRAIT,
Ti.UI.UPSIDE_PORTRAIT
];
return win;
}
};
//Bring in the child windows
Ti.include('main.js','camera.js');
//Create our configs
var winConfig = {
title:Ti.Locale.getString('sample_win_title'),
barColor:'#000',
backgroundColor:'#fff',
tabBarHidden:true,
fullscreen:false,
navBarHidden : (Ti.Platform.name == 'android')
};
//Create the main launch window
demo.mainWindow = demo.launchMainWindow(winConfig);
//Check if we didn't return the window correctly
if(demo.mainWindow===null){
alert('Forgot to return the window...');
}
//Based on platfomr launch the App in a specific way
if(Ti.Platform.name!=='android'){
var tabGroup = Ti.UI.createTabGroup();
var tab1 = Ti.UI.createTab({window:demo.mainWindow});
tabGroup.addTab(tab1);
// open tab group
tabGroup.open();
}else{
demo.mainWindow.open();
}
demo.launchCameraWindow=function(winConfig,successCallback,cancelCallback,failureCallback){
//We use our helper functions to create the window
var win = demo.myHelpers.makeWindow(winConfig);
Ti.Media.showCamera({
success:function(event){
var image = event.media;
successCallback(image)
},
cancel:function(){
cancelCallback();
},
error:function(error){
failureCallback(error.code;)
},
saveToPhotoGallery:false,
allowEditing:true,
mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
});
//-------------------------------------------------------------
// Don't forget to return the window
//-------------------------------------------------------------
return win;
};
demo.launchMainWindow=function(winConfig){
//Use the helper function to build a window
//This the easiest way to apply defaults
var win = demo.myHelpers.makeWindow(winConfig);
//set the layout to vertical, since in this window it makes life easier
win.layout='vertical';
var myWinConfig = {
title:'Take a picture',
barColor:'#000',
backgroundColor:'#fff',
tabBarHidden:true,
navBarHidden : (Ti.Platform.name == 'android')
};
var bSampleWithCallback = Ti.UI.createButton({
title:'Click Me',
height:45,
left:10,
right:10,
top:100
});
win.add(bSampleWithCallback);
function successCallback(myValue){
Ti.API.info("We're now in the Success callback function");
};
function cancelCallback(myValue){
Ti.API.info("We're now in the Cancel callback function");
};
function errorCallback(myValue){
Ti.API.info("We're now in the Error callback function");
};
//Add the event handler for the callback sample
bSampleWithCallback.addEventListener('click', function(e) {
var myCameraWindow = demo.launchCameraWindow(pickerWinConfig,successCallback,cancelCallback,errorCallback);
if(myCameraWindow===null){
alert('Forgot to return the window);
}
myCameraWindow.open({modal:true});
});
//-------------------------------------------------------------
// Don't forget to return the window
//-------------------------------------------------------------
return win;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment