How to load image asynchronously from server in titanium ?

You must Login before you can answer or comment on any questions.

I have a web service, which returns continuously a bunch of data from service. Each data contains a image url. Now I want to generate table using data but image will be loaded one by one after generating table. Any idea or source code help needed.

Thanks in advance :)

2 Answers

Accepted Answer

Try using this code

var my_image = new_cached_image('images/biguserimage.png', modified_url);
 
 
function new_cached_image( default_image, modified_url) {
    Ti.include('common/cachedImageView.js');
    var cached_image = Ti.UI.createImageView({
        image : default_image,
    });
    cachedImageView('cached_images', modified_url, cached_image);
    return cached_image;
};
and cachedImageView.js
/*
 Developed by Kevin L. Hopkins (http://kevin.h-pk-ns.com)
 
 
 Expects parameters of the directory name you wish to save it under, the url of the remote image,
 and the Image View Object its being assigned to. */
 
cachedImageView = function(imageDirectoryName, url, imageViewObject) {
    // Grab the filename
    var filename = url.split('/');
    filename = filename[filename.length - 1];
    // Try and get the file that has been previously cached
    var file = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName, filename);
 
    if (file.exists()) {
        // If it has been cached, assign the local asset path to the image view object.
        imageViewObject.image = file.nativePath;
    } else {
        // If it hasn't been cached, grab the directory it will be stored in.
        var g = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, imageDirectoryName);
        if (!g.exists()) {
            // If the directory doesn't exist, make it
            g.createDirectory();
        };
 
        // Create the HTTP client to download the asset.
        var xhr = Ti.Network.createHTTPClient();
 
        xhr.onload = function() {
            if (xhr.status == 200) {
                // On successful load, take that image file we tried to grab before and
                // save the remote image data to it.
                file.write(xhr.responseData);
                // Assign the local asset path to the image view object.
                imageViewObject.image = file.nativePath;
            };
        };
 
        // Issuing a GET request to the remote URL
        xhr.open('GET', url);
        // Finally, sending the request out.
        xhr.send();
    };
};

Hope this helps. Working absolutely fine for me.

— answered 10 months ago by Sahil Grover
answer permalink
3 Comments
  • Actually the data and images come from service,1st all text data get into the table and images are loading asynchronously. 1st 3 images loaded then 3 , like this way. So it would be helpful if you guyz share any source code. Thanks in advance :)

    — commented 10 months ago by Md. Rashed Azad Chowdhury

  • your idea is nice, just trying to implement it.

    — commented 10 months ago by Md. Rashed Azad Chowdhury

  • @Rashed whatever you are implementing please write over here so that it can help others or in case you are using some of the answers given here please accept the Answer

    — commented 10 months ago by Sahil Grover

Hi

Firstly the answer provided by Sahil is excellent, but in case you are not looking to cache the images locally I thought I would add my thoughts.

IF all you are after is to have images appear from a remote url, then simply reference that url in an imageview, the oading of images is automatic and will only occur when they come into view - so you do not need to handle their download manually.

var image = Ti.UI.createImageView({
    image:'http://example.com//images/myimage.png'
});
You can of course add these to tableviewrows as a leftImage, rightImage or a view added with a view (as above).
var row = Ti.UI.createTableViewRow({
  title: 'Title 1',
  leftImage: 'http://example.com//images/myimage1.png',
  rightImage: 'http://example.com//images/myimage2.png'
});
If you add 200 hundred images but only 10 are visible then only 10 will be loaded, when other images come into the visual space then those are also loaded.

— answered 10 months ago by Malcolm Hollingsworth
answer permalink
3 Comments
  • Actually the data and images come from service,1st all text data get into the table and images are loading asynchronously. 1st 3 images loaded then 3 , like this way. So it would be helpful if you guyz share any source code. Thanks in advance :)

    — commented 10 months ago by Md. Rashed Azad Chowdhury

  • if you share full source code of your idea then it will e helpful for my task.

    — commented 10 months ago by Md. Rashed Azad Chowdhury

  • @Malcolm thanks for being +1 with my answer.

    I have just done image caching because in case you are fetching the same imgaes again and again it will save a lot of traffic over network. Fetching some JSON / XML or some other data doesnt take that much load on network.

    Caching of images give application a better experince next time when same image is needed.

    — commented 10 months ago by Sahil Grover

Your Answer

Think you can help? Login to answer this question!