-
-
Save skypanther/1901680 to your computer and use it in GitHub Desktop.
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); |
It would help if this code worked!
On Android 4.4.3:
[ERROR] TiApplication: (main) [46,2159] Sending event: exception on thread: main msg:java.lang.IllegalArgumentException: bitmap size exceeds 32bits; Titanium 3.3.0,2014/07/11 12:36,787c
d39
[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:810)
[ERROR] TiApplication: at android.graphics.Bitmap.createBitmap(Bitmap.java:787)
[ERROR] TiApplication: at android.graphics.Bitmap.createBitmap(Bitmap.java:754)
[ERROR] TiApplication: at org.appcelerator.titanium.util.TiUIHelper.viewToImage(TiUIHelper.java:681)
[ERROR] TiApplication: at org.appcelerator.titanium.view.TiUIView.toImage(TiUIView.java:1647)
[ERROR] TiApplication: at org.appcelerator.titanium.proxy.TiViewProxy.handleToImage(TiViewProxy.java:902)
[ERROR] TiApplication: at org.appcelerator.titanium.proxy.TiViewProxy.handleMessage(TiViewProxy.java:273)
[ERROR] TiApplication: at android.os.Handler.dispatchMessage(Handler.java:98)
[ERROR] TiApplication: at android.os.Looper.loop(Looper.java:136)
[ERROR] TiApplication: at android.os.Handler.dispatchMessage(Handler.java:98) [208/9326]
[ERROR] TiApplication: at android.os.Looper.loop(Looper.java:136)
[ERROR] TiApplication: at android.app.ActivityThread.main(ActivityThread.java:5001)
[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:785)
[ERROR] TiApplication: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
[ERROR] TiApplication: at dalvik.system.NativeStart.main(Native Method)
[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)
What if I do not want to cache the image. By default it caches then how to override it?
What is the objective of this method if Titanium has done remote image caching itself? Is this still necessary?
Thanks