Can photos be resized on the device?
I am developing an app that allows users to upload photos from their iPhone photo gallery and camera app. Unfortunately, these images can be quite large and take a long time to upload, even over a 3G connection.
Is there any way to resize the photo on the device before sending it to the server?
Thanks in advance :)
7 Answers
-
Accepted Answer
Try this, it seems to work…
Titanium.Media.openPhotoGallery({ success:function(event) { var image = event.media; Ti.API.info(image.height +' x '+ image.width); var imageView = Titanium.UI.createImageView({ image:image, width:480, height:640 }); image = imageView.toImage(); Ti.API.info(image.height + " x " + image.width); }});
-
You can use Appcelerator's free ImageFactory module from the market place:
https://marketplace.appcelerator.com/apps/1184
-
I'm using
e.media.imageAsResized(e.media.width / 2, e.media.height / 2);
to cut the image height/width in half. Worth a shot -
Check out this one
// testend on iOS 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] });
-
Hi,
Javascript ist capable of resizing images, even in a content aware fashion:
http://labs.pimsworld.org/wp-content/uploads/2009/04/demo-content-aware-image-resizing-2/Search for javascript, canvas, resize on your favorite search engine :)
-
This only works if you're working directly with a captured image.
I was looking for a generic resize and crop of images once loaded into an image view, but no such luck – at least not on android. There is an undocumented function that does help ios only and I updated another post with some android module code.
-
yes, there are actually more than one way to do this.
http://docs.appcelerator.com/titanium/latest/#!/api/Titanium.Blob-method-imageAsResizedimageAsResized, imageAsCropped,imageAsThumbnail
var blob = f.read(); photo = blob.imageAsResized(blob.width / 4, blob.height / 4);