Search code examples
titaniumtitanium-mobileappceleratorappcelerator-mobile

Can any one give me ideas to convert an image (remote url) into base64 in mobile web titanium?


Can any one give me ideas to convert an image (remote url) into base64 in mobile web titanium . I tried

var data=Ti.Utils.base64encode(remoteUrl);

which converts remote url into base64 rather than the image exists in that url. Can any one share your ideas.

Thanks in Advance, Swathi.


Solution

  • The method you have used (base64encode) accept either a String, a Blob object or a Filesystem.File as a parameter. You're trying to encode a String (your remoteUrl) but this isn't what you actually want!!

    You need to get the image, and then you can use it as a parameter for that method. So just request the image and then pass it to the method to get what you want.

    var client = Ti.Network.createHTTPClient();
    client.onload = function() {
        var base64String = Titanium.Utils.base64encode(this.responseData).getText();
    };
    client.open("GET", "http://your.remote.url");
    client.send();
    

    Note that after having used the base64encode method you have a Blob object. If you want to get a String just use the getText method!

    You can find more info about the correct use of the base64encode method here.