简体   繁体   English

使用Appcelerator Titanium缩放图像

[英]Scale image using Appcelerator Titanium

Is there some easy way to scale an image to a maximum size? 有什么简单的方法可以将图像缩放到最大尺寸?

Looking for some some easy way to keep file sizes down and scale images that are uploaded from the app to max 640 width for example. 寻找一些简单的方法来减小文件大小,并将从应用程序上传的图像缩放到最大640宽度。

I have googled several solutions, from module Ti.ImageFactory to just put it into an ImageView with a size, and get the blob back again (which should make it scaled?). 我已经用Google搜索了几种解决方案,从模块Ti.ImageFactory到将其放入具有一定大小的ImageView中,然后再次获取Blob(这应该使它缩放)。 The Ti.ImageFactory module seems very old though, several years ago updated if you look at GIT. Ti.ImageFactory模块似乎很旧,几年前如果您查看GIT,则对其进行了更新。 Also the zip files for download seems to be missing on git... 此外,git上似乎缺少要下载的zip文件。

Ex. 例如

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]
        });

What you are looking for is the function imageAsCropped or imageAsResized . 您要查找的是函数imageAsCroppedimageAsResize

Let me know if you need Further help. 让我知道您是否需要进一步的帮助。

You can use the size property. 您可以使用size属性。

size : Dimensionreadonly 尺寸:尺寸只读

The size of the view in system units. 视图的大小(以系统单位)。

Although property returns a Dimension dictionary, only the width and height properties are valid. 尽管属性返回Dimension字典,但是只有width和height属性有效。 The position properties--x and y--are always 0. 位置属性-x和y-始终为0。

To find the position and size of the view, use the rect property instead. 要查找视图的位置和大小,请改用rect属性。

The correct values will only be available when layout is complete. 正确的值仅在布局完成后才可用。 To determine when layout is complete, add a listener for the postlayout event. 要确定何时完成布局,请为postlayout事件添加一个侦听器。

Documentation Link 文档链接

I ended up using ti.imagefactory module. 我最终使用了ti.imagefactory模块。 The zip files for android and ios I found here: https://github.com/appcelerator-modules/ti.imagefactory/releases 我在这里找到的android和ios的zip文件: https : //github.com/appcelerator-modules/ti.imagefactory/releases

Example can be found here: 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

I used code from the examples in my app and it seems to work fine! 我在我的应用程序中使用了示例中的代码,它看起来工作正常!

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

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