Posted By

spawnrider on 12/06/11


Tagged

action script cache


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

burnandbass
panosbranis


AS3 - Cached image from URL


 / Published in: ActionScript 3
 

Retrieve remote image from an URL and store it locally. Use this class for managing offline mode.

  1. package components
  2. {
  3. import com.adobe.crypto.MD5;
  4.  
  5. import flash.events.Event;
  6. import flash.filesystem.File;
  7. import flash.filesystem.FileMode;
  8. import flash.filesystem.FileStream;
  9. import flash.net.URLLoader;
  10. import flash.net.URLLoaderDataFormat;
  11. import flash.net.URLRequest;
  12. import spark.components.Image;
  13.  
  14. public class CachedImage extends Image
  15. {
  16.  
  17. private var _imageIdentifier:String = "";
  18. private var urlLoader:URLLoader = new URLLoader();
  19. private var _fromCache:Boolean = false;
  20.  
  21. public function CachedImage()
  22. {
  23. super();
  24. }
  25.  
  26. public function get fromCache():Boolean
  27. {
  28. return _fromCache;
  29. }
  30.  
  31. public function set fromCache(value:Boolean):void
  32. {
  33. _fromCache = value;
  34. }
  35.  
  36. public function get imageIdentifier():String
  37. {
  38. return _imageIdentifier;
  39. }
  40.  
  41. public function set imageIdentifier(value:String):void
  42. {
  43. _imageIdentifier = value;
  44. }
  45.  
  46. override public function get source():Object
  47. {
  48. return super.source;
  49. }
  50.  
  51. override public function set source(value:Object):void
  52. {
  53. if(!_fromCache) {
  54. if(value is Class){
  55. // do nothing
  56. } else if(value is String || value is Object){
  57. //Load cached image
  58. _imageIdentifier = MD5.hash(String(value));
  59.  
  60. // Cache the image
  61. var cacheFile:File= File.applicationStorageDirectory.resolvePath(_imageIdentifier);
  62. if (cacheFile.exists) {
  63. trace("ImageCache - Load Cached image from : "+cacheFile.url);
  64. _fromCache = true;
  65. super.source = cacheFile.url;
  66. return;
  67. } else {
  68. trace("ImageCache - Cached image not found, create it");
  69. urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
  70. urlLoader.load(new URLRequest(String(value)));
  71. urlLoader.addEventListener(Event.COMPLETE,imageLoaded);
  72. return;
  73. }
  74. }
  75. }
  76. trace("ImageCache - Load image from : "+value);
  77. super.source = value;
  78. }
  79.  
  80. // when remote image is loaded, cache it !
  81. protected function imageLoaded(event:Event):void {
  82. var tilefile:File = File.applicationStorageDirectory.resolvePath(_imageIdentifier);
  83. if (tilefile.exists) {
  84. trace("ImageCache - Already exist, recreate it");
  85. tilefile.deleteFile();
  86. }
  87. trace("ImageCache - Cache image");
  88. var fileStream:FileStream = new FileStream();
  89. fileStream.open(tilefile, FileMode.WRITE);
  90. fileStream.writeBytes(urlLoader.data);
  91. fileStream.close();
  92. _fromCache = true;
  93. super.source = tilefile.url;
  94. }
  95. }
  96. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Retrocadenet on December 10, 2011

It is worth noting it is Adobe AIR-only solution

Posted By: panosbranis on April 24, 2014

Very good class, but how do you use it?..? thank you!

Posted By: panosbranis on April 24, 2014

Very good class, but how do you use it?..? thank you!

Posted By: panosbranis on April 24, 2014

Very good class, but how do you use it?..? thank you!

You need to login to post a comment.