AS3-来自URL的缓存图像

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了AS3-来自URL的缓存图像相关的知识,希望对你有一定的参考价值。

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. }

以上是关于AS3-来自URL的缓存图像的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Picasso 使用动态 URL 缓存来自 S3 存储桶的图像?

来自 url 的按钮图像未显示

ActionScript 3 来自AS3的PHP图像上传器

图像源和缓存

从客户端URL下载as3中的图像

仅允许缓存来自特定网址的图片