【问题标题】:How to Compress the IMAGE size in Titanium Appcelerator如何在 Titanium Appcelerator 中压缩图像大小
【发布时间】:2012-06-14 08:53:21
【问题描述】:

我使用的是钛金属 2.0.1,我需要压缩相机拍摄的图像大小然后上传。目前图像大小为 800kb+,上传需要很长时间。我需要压缩大小。谁能告诉我如何做到这一点。

【问题讨论】:

    标签: image upload compression titanium appcelerator


    【解决方案1】:

    默认情况下,Titanium 似乎以“高”质量导出图像,而无法将压缩设置调整为较低质量(没有第三方模块)。如果您将 Titanium 生成的 JPG 与通过 Photoshop 的 Save for Web JPEG 'High' 功能导出的 JPG 进行比较,您会发现 Ti 图像的文件大小要大得多。

    您可以尝试以下模块之一:

    iOS 和安卓: market.appcelerator.com/apps/1184?540167410

    仅限 iOS: https://github.com/gudmundurh/titanium-imaging

    仅限安卓: https://github.com/novelys/titanium-jpegencoder

    【讨论】:

    • 我尝试过使用android模块..但它在我的tiapp.xml中不支持..我应该如何使用它..?
    【解决方案2】:

    图像不会压缩太多,因为许多图像格式已经压缩了图像,例如 jpeg。我在 3118 KB 的 jpg 图像上尝试了 zip 和 7zip,然后 zip 将其压缩到 3114 KB,而 7zip 将其大小增加到 3121 KB。

    如果您仍想压缩图像大小,可以尝试使用以下 javascript 代码进行 zip 压缩:https://github.com/TermiT/ZipFile。这可能会使您的上传时间更慢,因为您必须等待应用压缩图像并等待应用上传。

    如果您不介意上传尺寸较小的图像,这也会使文件大小更小,您可以使用 Titanium 的 imageAsResized 方法。在 Titanium 2.0 之前,该方法在 Android 中不起作用。我还没有在 Titanium 2.0 中测试过它现在是否可以在 Android 中运行。

    您可能还想查看网络连接速度(无线、3G、4G)。也许您的测试连接速度很慢。

    【讨论】:

    • 这是错误的。 OP 正在询问通过降低图像质量来减小图像大小。 ZIP 不会改变图像的大小是正确的,但重新编码会。
    【解决方案3】:

    赞成的答案似乎已经过时了。

    https://github.com/appcelerator-modules/ti.imagefactory 是在 iOS 和 Android 上运行的最新模块,允许使用 Appcelerator/Axway/Titanium 进行图像处理。

    来自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();
    

    });

    【讨论】: