Skip to content

Instantly share code, notes, and snippets.

@jonalter
Created February 10, 2012 16:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonalter/1790631 to your computer and use it in GitHub Desktop.
Save jonalter/1790631 to your computer and use it in GitHub Desktop.
iOS: convert view layout to single image using toImage()
var win = Ti.UI.createWindow({backgroundColor:'white'});
win.open();
// build our layout with 2 views and 1 imageView
var mainView = Ti.UI.createView({top:10,height:200,width:200,backgroundColor:'blue'});
var view2 = Ti.UI.createView({height:100,width:100,backgroundColor:'green'});
var imageView = Ti.UI.createImageView({image:'http://www.appcelerator.com/wp-content/uploads/2011/01/Appcelerator-IDC-Q1-Mobile-Developer-Report-appclogo.png'});
mainView.add(view2);
view2.add(imageView);
// this imageView will display the entire layout from above but will be lighter because it is only one imageView
var singleImage = Ti.UI.createImageView({top:220,height:200,width:200,backgroundColor:'red'});
win.add(singleImage);
convertViewToBlob(mainView, imageView, function(blob){
singleImage.image = blob;
});
// will do toImage 2x: once immediately and then again when 'imageToLoad' loads
function convertViewToBlob(view, imageToLoad, callback){
imageToLoad.addEventListener('load', runOnLoad);
function runOnLoad(){
imageToLoad.removeEventListener('load', runOnLoad);
convertView(true);
}
view.top = 2000;
convertView();
function convertView(/*bool*/ isDestructive){
var blob = view.toImage();
if(callback){
callback(blob);
}
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment