0
votes

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]
        });
3
You got to provide more info than this. What have you tried already? Where are you stuck? What is your code? - Rene Pot
I simply have an image blob which i want to resize to a maximum size. I have no code for it yet, I'm asking for the best way to do this. Use a module? Ti.ImageFactory (at github which some people suggest, seems waaaay old). Put it in an imageView with a size and get the image back again? - Emil
On stackoverflow you're required to provide what you've tried and what code you have. Usually these kind of questions can be easily googled or asked to the Community - Rene Pot
I've put some more info into the question explaining where i need the image scaled... I've not found any good solutions with google so far... - Emil

3 Answers

0
votes

What you are looking for is the function imageAsCropped or  imageAsResized.

Let me know if you need Further help.

0
votes

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

0
votes

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!