Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Last active November 5, 2019 15:18
Show Gist options
  • Save MotiurRahman/57535845ebf40bd2041448991ebd92fc to your computer and use it in GitHub Desktop.
Save MotiurRahman/57535845ebf40bd2041448991ebd92fc to your computer and use it in GitHub Desktop.
RemoteImage Test
var win = Titanium.UI.createWindow({
className : 'win'
});
var Utils = {
    /* modified version of https://gist.github.com/1243697 */
    _getExtension: function(fn) {
        // CREDITS: http://stackoverflow.com/a/680982/292947
        var re = /(?:\.([^.]+))?$/;
        var tmpext = re.exec(fn)[1];
        return (tmpext) ? tmpext : '';
    },
    RemoteImage: function(a) {
        a = a || {};
        var md5;
        var needsToSave = false;
        var savedFile;
        if (a.image !== null) {
            md5 = Ti.Utils.md5HexDigest(a.image)+this._getExtension(a.image);
            savedFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory, md5);
            if (savedFile.exists()) {
                a.image = savedFile;
            } else {
                needsToSave = true;
            }
        }
        var image = Ti.UI.createImageView(a);
        if (needsToSave === true) {
            function saveImage(e) {
                image.removeEventListener('load', saveImage);
                savedFile.write(
                    Ti.UI.createImageView({ image: image.image, width: Ti.UI.FILL, height: Ti.UI.FILL }).toImage()
                );
            }
            image.addEventListener('load', saveImage);
        }
        return image;
    }
};
var img = Utils.RemoteImage({
image : 'http://ursinus.prestosports.com/sports/wbkb/2019-20/photos/KRR_48190-1.jpg?max_width=160&max_height=120',
top : 40,
defaultImage:'test.png',
width : 200,
height : 200
});
win.add(img);
img.addEventListener('load', function(){
Ti.API.info("ImageView:", img.toBlob());
});
win.open();
@MotiurRahman
Copy link
Author

[INFO] : I/Adreno-EGL: Reconstruct Branch: NOTHING
[INFO] : APSAnalyticsStore: session.end
[INFO] : APSAnalyticsService: Successfully sent 11 stored event(s)
[INFO] : APSAnalyticsService: Successfully sent 3 stored event(s)
[INFO] : TiVerify: (Timer-0) [4772,4997] Verifying module licenses...
[INFO] : TiVerify: (Timer-0) [2293,7290] Successfully verified module licenses
[INFO] : APSAnalyticsStore: session.start
[INFO] : Choreographer: Skipped 59 frames! The application may be doing too much work on its main thread.
[INFO] : APSAnalyticsService: Successfully sent 1 stored event(s)
[INFO] : ImageView: [object TiBlob]
[INFO] : ImageView: [object TiBlob]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment