简体   繁体   中英

How to Compress the IMAGE size in Titanium Appcelerator

i am using titanium 2.0.1, i need to compress the size of a image taken by camera and then upload it. Right now the image size is 800kb+, which takes a lot of time to upload. i need to compress the size. can anyone tell me the way how to do that.

Titanium seems to export images at 'High' quality by default with no way to adjust the compression settings to a lower quality (without a third party module). If you compare the Titanium generated JPG against one exported via Photoshop's Save for Web JPEG 'High' feature you'll notice the Ti image has a significantly larger filesize.

You could try one of these modules:

iOS and Android: marketplace.appcelerator.com/apps/1184?540167410

iOS Only: https://github.com/gudmundurh/titanium-imaging

Android Only: https://github.com/novelys/titanium-jpegencoder

Images don't compress much because many image formats already compress the image, like jpeg. I tried zip and 7zip on a 3118 KB jpg image and zip compressed it to 3114 KB while 7zip increased its size to 3121 KB.

If you still want to compress the image size you can try this javascript code for zip compression: https://github.com/TermiT/ZipFile . It will probably make your upload time even slower because you will have to wait for the app to compress the image and wait for the app to upload it.

If you don't mind uploading an image with smaller dimensions, which also makes the file size smaller, you can use Titanium's imageAsResized method. That method didn't work in Android before Titanium 2.0. I haven't tested it in Titanium 2.0 to see if it now works in Android.

Something else you may want to look at is the network connection speed (wireless, 3G, 4G). Maybe your test was on a slow connection.

The upvoted answer seems to be outdated.

https://github.com/appcelerator-modules/ti.imagefactory is an up-to-date module that runs on iOS and Android and allows for image manipulation with Appcelerator/Axway/Titanium.

Some example from https://github.com/appcelerator-modules/ti.imagefactory/blob/stable/ios/example/app.js

btnSave.addEventListener('click', function (e) {
newBlob = ImageFactory.compress(blob, 0.25);
var filename = Titanium.Filesystem.applicationDataDirectory + "/newflower.jpg";
f = Titanium.Filesystem.getFile(filename);
f.write(newBlob);
var alert = Ti.UI.createAlertDialog({
    title:'Image Factory',
    message:'Compressed image saved to newflower.jpg with compression quality of 25%'
});
alert.show();

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM