简体   繁体   中英

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.

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?). The Ti.ImageFactory module seems very old though, several years ago updated if you look at GIT. Also the zip files for download seems to be missing on git...

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 .

Let me know if you need Further help.

You can use the size property.

size : Dimensionreadonly

The size of the view in system units.

Although property returns a Dimension dictionary, only the width and height properties are valid. The position properties--x and y--are always 0.

To find the position and size of the view, use the rect property instead.

The correct values will only be available when layout is complete. To determine when layout is complete, add a listener for the postlayout event.

Documentation Link

I ended up using ti.imagefactory module. The zip files for android and ios I found here: 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

I used code from the examples in my app and it seems to work fine!

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