Stream Image Loading

In ActionScript 1-2, we often needed to create preloader for images, because we couldn’t show them while loading were in progress. ActionScript 3.0 changes this, with ByteArray and URLStream classes we can work with bytes instead of files.

That means that we can show half of the image, or different states of progressive JPEG file.

This is StreamLoader class, which loads image using URLStream class, and passes ByteArray to Loader class.

package eu.orangeflash.display
{
 import flash.display.Loader;
 import flash.events.*;
 import flash.net.*;
 import flash.utils.*;
 import flash.system.LoaderContext;
 
 [Event(name = "complete", type="flash.events.Event")]
 [Event(name = "progress", type="flash.events.ProgressEvent")]
 [Event(name = "open", type="flash.events.Event")]
 [Event(name = "ioError", type="flash.events.IOErrorEvent")]
 
 public class StreamLoader extends Loader
 {
 protected var stream:URLStream = new URLStream();
 protected var bytes:ByteArray = new ByteArray();
 protected var cont:LoaderContext;
  
 import mx.core.UIComponent
  /**
   * Constructor, creates new StreamLoader
   * 
   */
 public function StreamLoader(request:URLRequest = null)
  {
   if(request != null)
   {
    loadStream(request);
   }
  }
  /**
   * Method, loads images as stream
   * 
   * @param request  URLRequest
   * @param context  
LoaderContext, default value is null
   */
  public function loadStream(request:URLRequest, 
context:LoaderContext = null):void
  {
   cont = context;
   
   stream.load(request);
   
stream.addEventListener(Event.COMPLETE,onStreamComplete);
stream.addEventListener(ProgressEvent.PROGRESS,onStreamProgress);
stream.addEventListener(Event.OPEN,onStreamOpen);
stream.addEventListener(IOErrorEvent.IO_ERROR,onStreamIOError);
stream.addEventListener(HTTPStatusEvent.HTTP_STATUS,
onStreamHTTPStatus);
 stream.addEventListener(SecurityErrorEvent.
SECURITY_ERROR,onStreamSecurityError);
  }
  /**
   * @private
   */
  private function onStreamComplete(event:Event):void
  {
   updateBytes();
   dispatchEvent(new Event(event.type, event.
bubbles, event.cancelable));
   
   setTimeout(confirmBytesLoaded,50);   
  }
  /**
   * @private
   */
  private function onStreamProgress(event:ProgressEvent)
                                   :void
  {
  updateBytes();
  dispatchEvent(new ProgressEvent(event.type, event.
                            bubbles,
   event.cancelable, event.bytesLoaded, 
                           event.bytesTotal));
   
  }
  /**
   * @private
   */
  private function onStreamIOError(event:IOErrorEvent)
                                   :void
  {
  dispatchEvent(new IOErrorEvent(event.type, 
                  event.bubbles, event.cancelable, event.text));
  }
  /**
   * @private
   */
  private function onStreamSecurityError(event:Securit
yErrorEvent):void
  {
  dispatchEvent(new SecurityErrorEvent(event.type, 
event.bubbles, event.cancelable, event.text));
  }
  /**
   * @private
   */
  private function onStreamHTTPStatus(event:HTTPStat
                          usEvent):void
  {
  dispatchEvent(new HTTPStatusEvent(event.type, event
.bubbles, event.cancelable, event.status));
  }
  /**
   * @private
   */
  private function onStreamOpen(event:Event):void
  {
  dispatchEvent(new Event(event.type, 
                   event.bubbles, event.cancelable));
  }
  /**
   * @private
   */
  private function updateBytes():void
  {
   stream.readBytes(bytes, bytes.length);
   if(bytes.length > 0)
   {
    loadBytes(bytes,cont);
   }
  }
  /**
   * @private
   * 
   * Method confirms that all bytes loaded, and closes stream
   */
  private function confirmBytesLoaded():void
  {
   updateBytes();
   stream.close();
  }
  
 }
}

In the example below, you can see normal JPEG file on your left, and p
rogressive on your right. Note you have to be 18 years old to view the
content :)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值