使用 Appcelerator Titanium 缩放图像

Scale image using Appcelerator Titanium

是否有一些简单的方法可以将图像缩放到最大尺寸?

寻找一些简单的方法来减小文件大小并将从应用程序上传的图像缩放到最大 640 宽度。

我用谷歌搜索了几个解决方案,从模块 Ti.ImageFactory 到只是将其放入具有大小的 ImageView 中,然后再次取回 blob(这应该使它缩放?)。 Ti.ImageFactory 模块看起来很旧,如果您查看 GIT,它是几年前更新的。 git...

上似乎缺少下载的 zip 文件

例如

Titanium.Media.openPhotoGallery({
            success:function(event)
            {
               Ti.API.info('Our type was: '+event.mediaType);

               if(event.mediaType == Ti.Media.MEDIA_TYPE_PHOTO)
               {

                   // HERE event.media needs to be scaled to a max
                   // size... for example 2 MB data, or max 960 px 
                   // width/height or something similar 

                   uploadPhoto(event.media);

               }
            },
            cancel:function()
            {
                Ti.API.info("Photo gallery cancel...");
            },
            error:function(err)
            {
                //Ti.API.error(err);
                Ti.API.info("Err: "+err);
                alert(L("AN_ERROR_OCCURRED"));
            },
            mediaTypes:[Ti.Media.MEDIA_TYPE_PHOTO]
        });

您要查找的是函数 imageAsCroppedimageAsResized.

如果您需要进一步的帮助,请告诉我。

您可以使用尺寸 属性。

size : Dimensionreadonly

以系统单位表示的视图大小。

虽然属性 returns一个维度字典,只有宽度和 高度属性有效。位置属性——x 和 y——是 总是 0.

要查找视图的位置和大小,请使用 rect 属性 相反。

正确的值只有在布局完成后才可用。到 确定布局何时完成,为后布局添加一个监听器 事件。

Documentation Link

我最终使用了 ti.imagefactory 模块。 我在这里找到 android 和 ios 的 zip 文件: https://github.com/appcelerator-modules/ti.imagefactory/releases

示例可在此处找到: https://github.com/appcelerator-modules/ti.imagefactory/blob/stable/ios/example/app.js

我在我的应用程序中使用了示例中的代码,它似乎工作正常!