Skip to content

Instantly share code, notes, and snippets.

@pec1985
Created March 10, 2011 23:04
Show Gist options
  • Save pec1985/865141 to your computer and use it in GitHub Desktop.
Save pec1985/865141 to your computer and use it in GitHub Desktop.
Resize images from the Image Gallery
// testend on iOS
/*
Copyright 2011 Pedro Enrique
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
var win = Ti.UI.createWindow();
win.open();
// the desired image widths
var imageSizes = [1200,800,600,200];
// open the photo gallery
Titanium.Media.openPhotoGallery({
// if success:
success:function(event){
// this is the image chosen
var myImage = event.media;
if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO){
// let's loop through that array
for(var i = 0; i < imageSizes.length; i++){
// create a new file
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, 'image'+i+'.png');
// get the width from the array
var newWidth = imageSizes[i];
// write the file as toImage()
file.write(
Ti.UI.createImageView({
image:myImage, //file name
width:newWidth, // file new width
height:(newWidth/myImage.width)*myImage.height // some math to get the height
}).toImage()
);
// done, loop again
}
} else {
}
},
cancel:function(){
},
error:function(error){
},
mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment