简体   繁体   English

如何压缩Titanium Appcelerator中的IMAGE大小

[英]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. 我正在使用钛2.0.1,我需要压缩相机拍摄的图像的大小,然后上传它。 Right now the image size is 800kb+, which takes a lot of time to upload. 现在图像大小为800kb +,需要花费大量时间上传。 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). Titanium似乎默认以“高”质量导出图像,无法将压缩设置调整到较低质量(没有第三方模块)。 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. 如果将Titanium生成的JPG与通过Photoshop的Save for Web JPEG“High”功能导出的JPG进行比较,您会发现Ti图像的文件大小要大得多。

You could try one of these modules: 您可以尝试以下模块之一:

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

iOS Only: https://github.com/gudmundurh/titanium-imaging 仅适用于iOS: https//github.com/gudmundurh/titanium-imaging

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

Images don't compress much because many image formats already compress the image, like jpeg. 图像不会压缩太多,因为许多图像格式已经压缩图像,如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. 我在3118 KB jpg图像上尝试了zip和7zip,zip压缩到3114 KB,而7zip将其大小增加到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 . 如果您仍想压缩图像大小,可以尝试使用此javascript代码进行压缩: 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. 如果您不介意上传尺寸较小的图像,这也会使文件尺寸变小,您可以使用Titanium的imageAsResized方法。 That method didn't work in Android before Titanium 2.0. 在Titanium 2.0之前,该方法在Android中无效。 I haven't tested it in Titanium 2.0 to see if it now works in Android. 我还没有在Titanium 2.0中测试它,看看它现在是否适用于Android。

Something else you may want to look at is the network connection speed (wireless, 3G, 4G). 你可能想要看的其他东西是网络连接速度(无线,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. https://github.com/appcelerator-modules/ti.imagefactory是一个在iOS和Android上运行的最新模块,允许使用Appcelerator / Axway / Titanium进行图像处理。

Some example from https://github.com/appcelerator-modules/ti.imagefactory/blob/stable/ios/example/app.js 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();

}); });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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