-
-
Save skypanther/1901680 to your computer and use it in GitHub Desktop.
Cache Remote Images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Utils = { | |
/* modified version of https://gist.github.com/1243697 | |
* adds detection of file extension rather than hard-coding .jpg as in the original | |
*/ | |
_getExtension: function(fn) { | |
// from 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){ | |
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:'auto',height:'auto'}).toImage() | |
); | |
} | |
image.addEventListener('load',saveImage); | |
} | |
return image; | |
} | |
}; | |
// example usage | |
var image = Utils.RemoteImage({ | |
image:'http://farm7.staticflickr.com/6059/6262552465_e53bccbd52_z.jpg', | |
defaultImage:'KS_nav_ui.png', | |
width:300, | |
height:200, | |
top:20 | |
}); | |
win.add(image); |
What if I do not want to cache the image. By default it caches then how to override it?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
[ERROR] : TiApplication: (main) [7450,12960] Sending event: exception on thread: main msg:java.lang.IllegalArgumentException: bitmap size exceeds 32bits; Titanium 4.1.1,2015/08/16 21:42,7e39876
[ERROR] : TiApplication: java.lang.IllegalArgumentException: bitmap size exceeds 32bits
[ERROR] : TiApplication: at android.graphics.Bitmap.nativeCreate(Native Method)
[ERROR] : TiApplication: at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
[ERROR] : TiApplication: at android.graphics.Bitmap.createBitmap(Bitmap.java:786)
[ERROR] : TiApplication: at android.graphics.Bitmap.createBitmap(Bitmap.java:753)
[ERROR] : TiApplication: at org.appcelerator.titanium.util.TiUIHelper.viewToImage(TiUIHelper.java:764)
[ERROR] : TiApplication: at org.appcelerator.titanium.view.TiUIView.toImage(TiUIView.java:1686)
[ERROR] : TiApplication: at org.appcelerator.titanium.proxy.TiViewProxy.handleToImage(TiViewProxy.java:904)
[ERROR] : TiApplication: at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:274)
[ERROR] : TiApplication: at android.os.Handler.dispatchMessage(Handler.java:98)
[ERROR] : TiApplication: at android.os.Looper.loop(Looper.java:136)
[ERROR] : TiApplication: at android.app.ActivityThread.main(ActivityThread.java:5021)
[ERROR] : TiApplication: at java.lang.reflect.Method.invokeNative(Native Method)
[ERROR] : TiApplication: at java.lang.reflect.Method.invoke(Method.java:515)
[ERROR] : TiApplication: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
[ERROR] : TiApplication: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
[ERROR] : TiApplication: at dalvik.system.NativeStart.main(Native Method)