ADOBE/ ActionScript

[as3.0] 비트맵(Bitmap)이미지, 이미지 로드관련

AlrepondTech 2012. 1. 4. 20:37
반응형

 

 

 

=================================

=================================

=================================

 

 

 

 

 

 출처: 여기

     class CImage //
    {

        함수()
        {
            var u:URLRequest; // 그림
            var l:Loader;
            var asd:Bitmap = new Bitmap();
           
            u = new URLRequest();
            u.url = "resource/images/flag0.png";
            l = new Loader();
            l.load(u);
        
      
            addChild(l);
        } 
   }

//--------------------------------------------------------------------------------
출처: http://blog.naver.com/sumia?Redirect=Log&logNo=40105013066

외부이미지를 Loader로 부른 후 Bitmap 변환하여 보여준다.

 

package com.twohandsmedia.mainVisual.view
{
 import flash.display.Bitmap;
 import flash.display.BitmapData;
 import flash.display.Loader;
 import flash.display.Sprite
 import flash.events.Event;
 import flash.geom.Matrix;
 import flash.net.URLRequest;
 public class BitmapLoaders extends Sprite
 {  
  public function BitmapLoaders(_

url:String

)
  {
   var ldr:Loader = new Loader();
   ldr.load(new URLRequest(_

url

));
   ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, imgLoaded);
  }
  
  private function imgLoaded(e:Event):void
  {
   var bmp:Bitmap;
   var bd:BitmapData =  new BitmapData(e.target.content.width, e.target.content.height);
   bd.draw(e.target.content);
      
   bmp = new Bitmap(bd);
   addChild(bmp);
  }  
 }
}

//투명값 옵션 추가

윗부분에서 다음과 같이 추가.

BitmapData(e.target.content.width, e.target.content.height, true, 0xff0000);

 

//--------------------------------------------------------

출처: http://www.shop-wiz.com/board/main/view/root/flash01/290

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.net.URLRequest;

var url:String = "http://www.partplus.co.kr/images/userdif/car_slide/audi.png";
var size:uint = 80;



function configureAssets():void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
    loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    
    var request:URLRequest = new URLRequest(url);
    loader.x = size * numChildren;
    loader.load(request);
    addChild(loader);
}

function duplicateImage(original:Bitmap):Bitmap {
    var image:Bitmap = new Bitmap(original.bitmapData.clone());
    image.x = size * numChildren;
    addChild(image);
    return image;
}

function completeHandler(event:Event):void {
    var loader:Loader = Loader(event.target.loader);
    var image:Bitmap = Bitmap(loader.content);
    
    var duplicate:Bitmap = duplicateImage(image);
    var bitmapData:BitmapData = duplicate.bitmapData;
    var sourceRect:Rectangle = new Rectangle(0, 0, bitmapData.width, bitmapData.height);
    var destPoint:Point = new Point();
    var operation:String = ">=";
    var threshold:uint = 0xCCCCCCCC;
    var color:uint = 0xFFFFFF00;
    var mask:uint = 0x000000FF;
    var copySource:Boolean = true;
    
    bitmapData.threshold(bitmapData,
                         sourceRect,
                         destPoint,
                         operation,
                         threshold,
                         color,
                         mask,
                         copySource);
}

function ioErrorHandler(event:IOErrorEvent):void {
    trace("Unable to load image: " + url);
}
        
configureAssets();

 

///////////////////////////////////////////////////

출처: http://rash.tistory.com/entry/%EC%95%A1%EC%85%98%EC%8A%A4%ED%81%AC%EB%A6%BD%ED%8A%B8%EC%97%90%EC%84%9C-%EC%9D%B4%EB%AF%B8%EC%A7%80-%EB%8F%99%EC%A0%81-%EB%A1%9C%EB%94%A9Image-dynamic-loading-in-ActionScript

이미지 파일을 동적으로 읽어오는 작업은 특별히 어려운 점은 없으나

Loader, Bitmap, BitmapData 등의 변수가 필요하기 때문에
여러개의 이미지를 로딩하고자 하면 선언해야 할 변수가 너무 많아질 수 있다. (음.. Array 를 써서 간단히 할 수도 있겠지만)
 
------- MyBitmapLoader.as -------
package utils
{
import flash.events.EventDispatcher;
 
public class LoaderBitmap extends EventDispatcher
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.system.LoaderContext;
import flash.events.*;
import flash.net.URLRequest;
 
public var _loader:Loader = null;
public var _req:URLRequest = null;
public var _bitmapData:BitmapData = null;
public var _bitmap:Bitmap = null;
 
public function LoaderBitmap() 
{
_loader = new Loader();
}
 
public function Load(url:String):void{
 
_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, hCompleteHandler );    
_loader.contentLoaderInfo.addEventListener( IOErrorEvent.IO_ERROR, hError );
 
_req = new URLRequest( url );
 
var loaderContext:LoaderContext = new LoaderContext();
loaderContext.checkPolicyFile = true;
 
_loader.load(_req, loaderContext );
}
 
public function addCompletionEvent(func:Function):void{
_loader.contentLoaderInfo.addEventListener( Event.COMPLETE, func );
}
 
private function hError(e:IOErrorEvent):void
{
trace( "ImageLoader", "IOERROR", e );
 
_loader.contentLoaderInfo.removeEventListener( Event.COMPLETE, hCompleteHandler);    
_loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR, hError);
}
 
private function hCompleteHandler(event:Event):void
{
if( _loader.width == 0 )
return;
 
_bitmapData = new BitmapData( _loader.width, _loader.height, true, 0x00ff00ff );
_bitmapData.draw( _loader );
 
_loader.contentLoaderInfo.removeEventListener( Event.COMPLETE, hCompleteHandler);    
_loader.contentLoaderInfo.removeEventListener( IOErrorEvent.IO_ERROR, hError);
 
_loader = null;
_req = null;
_bitmap = new Bitmap(_bitmapData);
 
dispatchEvent( event );
}
}
}

------- MyFlexComponent.mxml -------
private var _loaderBg:LoaderBitmap = new LoaderBitmap();
 
public function LoadResource():void
{
_loaderBg.addEventListener(Event.COMPLETE, hCompleteBg);
_loaderBg.Load("http://" + Config.SOCKET_SERVER_IP + "/img/myImage.jpg" );
}
 
private function hCompleteBg(event:Event):void
{
_ui.addChild( _loaderBg._bitmap );
}
 

 

=================================

=================================

=================================

 

 

 
 
 
반응형