Skip to content

Instantly share code, notes, and snippets.

@pec1985
Created June 8, 2011 16:25
Show Gist options
  • Save pec1985/1014759 to your computer and use it in GitHub Desktop.
Save pec1985/1014759 to your computer and use it in GitHub Desktop.
Zoom in zoom out on a remote image
ar win = Ti.UI.createWindow({backgroundColor:'#676767'});
//FYI - this image is big, it will take a few seconds to appear
var file = 'http://bassemk.files.wordpress.com/2009/12/landscape-fall-creek-falls-and-snake-river-idaho.jpg';
var start=function(file){
var newHeight;
var newWidth;
var fileWidth=file.blob.width;
var fileHeight=file.blob.height;
if(fileWidth>fileHeight){
newHeight = fileHeight/(fileWidth/320);
newWidth = 320;
}else{
newHeight = 416;
newWidth = fileWidth/(fileHeight/416);
}
var scrollView = Titanium.UI.createScrollView({
contentWidth:'auto',
contentHeight:'auto',
height:416,
width:320,
showVerticalScrollIndicator:true,
showHorizontalScrollIndicator:true,
maxZoomScale:20
});
var image = Ti.UI.createImageView({image:file.file,width:file.blob.width,height:file.blob.height});
scrollView.add(image);
if(fileWidth>fileHeight){
scrollView.zoomScale=320/file.blob.width;
scrollView.minZoomScale=320/file.blob.width;
scrollView.contentOffset={x:0,y:0};
scrollView.contentWidth=newWidth;
scrollView.contentHeight=newHeight;
}else{
scrollView.zoomScale=416/file.blob.height;
scrollView.minZoomScale=416/file.blob.height;
scrollView.contentOffset={x:0,y:0};
scrollView.contentWidth=newWidth;
scrollView.contentHeight=newHeight;
}
win.add(scrollView);
var x = 0;
var newZoom=scrollView.zoomScale;
scrollView.addEventListener('scale',function(){
scrollView.contentWidth=(scrollView.scale/newZoom)*scrollView.contentWidth;
scrollView.contentHeight=(scrollView.scale/newZoom)*scrollView.contentHeight;
newZoom=scrollView.scale;
});
};
c = Titanium.Network.createHTTPClient();
c.setTimeout(10000);
c.onload = function(){
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,'foto.jpg');
f.write(this.responseData);
start({file:f,blob:this.responseData});
};
c.onerror = function(e){
alert(e);
};
Ti.API.info(file);
c.open('GET',file);
c.send();
win.open({modal:true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment