Search code examples
androidmemory-managementtitaniumappcelerator

load lot of images into titanium image view


I'm loading a lot of images into a image views in titanium. the problem is that in android I get memory problems. this is my function to put the image into the image view. In this function i'm resizing the image.

exports.SetImg = function(img, hightFactor, widthFactor, viewObj, deviceWidth, deviceHeight) {


    if (img != null && img != "") {
        var IMGhight = 0,
            IMGwidth = 0,
            height = 0,
            width = 0;

        var imgTemp = Ti.UI.createImageView({
            image : img
        });
        if (imgTemp != null) {

            if (imgTemp.toBlob().height != null && imgTemp.toBlob().width != null) {


                IMGhight = imgTemp.toBlob().height;
                IMGwidth = imgTemp.toBlob().width;
                height = alturaImg;
                width = larguraImg;

                if (height > deviceHeight * hightFactor) {
                    if (height > deviceHeight * hightFactor) {
                        height = deviceHeight * hightFactor;
                        width = (((deviceHeight * hightFactor) / IMGhight) * IMGwidth);
                    }

                    if (width < deviceWidth * widthFactor) {
                        width = deviceWidth * widthFactor;
                        height = (((deviceWidth * widthFactor) / IMGwidth) * IMGhight);
                    }
                } else if (width > deviceWidth * widthFactor) {
                    if (width > deviceWidth * widthFactor) {
                        width = deviceWidth * widthFactor;
                        height = (((deviceWidth * widthFactor) / IMGwidth) * IMGhight);
                    }

                    if (height < deviceHeight * hightFactor) {
                        height = deviceHeight * hightFactor;
                        width = (((deviceHeight * hightFactor) / IMGhight) * IMGwidth);
                    }
                } else if (height < deviceHeight * hightFactor) {
                    if (height < deviceHeight * hightFactor) {
                        height = deviceHeight * hightFactor;
                        width = (((deviceHeight * hightFactor) / IMGhight) * IMGwidth);
                    }

                    if (width < deviceWidth * widthFactor) {
                        height = deviceWidth * widthFactor;
                        height = (((deviceWidth * widthFactor) / IMGwidth) * IMGhight);
                    }
                } else if (width < deviceWidth * widthFactor) {
                    if (width < deviceWidth * widthFactor) {
                        width = deviceWidth * widthFactor;
                        height = (((deviceWidth * widthFactor) / IMGwidth) * IMGhight);
                    }
                    if (hight < deviceHeight * hightFactor) {
                        hight = deviceHeight * hightFactor;
                        width = (((deviceHeight * hightFactor) / IMGhight) * IMGwidth);
                    }
                }

                var imagem = Ti.UI.createImageView({
                    width : width,
                    height : hight,
                    image : img
                });

                viewObj.add(imagem);
                imagem = null;
                imgTemp = null;
                IMhight = null;
                IMGwidth = null;
                hight = null;
                width = null;
                img = null;
                hightFactor = null;
                widthFactor = null;
                viewObj = null;
                deviceWidth = null;
                deviceHeight = null;
            }
        }
    }
};

I'm already declare all variables to null for GC but I still get the memory problem. does some one now how to fix this?

This is the layout that I need do make.

the problem is that I need to reuse the same images for something else so I need them to bee beg.


Solution

  • You should really resize the image and save it to a thumbnail file. And not just resize the imageview and display the whole image in it. That would save you some memory.

    Also you are creating multiple blobs imgTemp.toBlob(). Run this command once and reuse the blob variable. The same for all the other calculations. It's not much but saves you some time/space when you just calculate it once and reuse the value.

    But you need to resize the image, perhaps with a custom android module which allows batch resizing with a whole folder or array of files if thats something you need.