• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How to manipulate a bitmap loaded through FileReference.load() ?

Engaged ,
Apr 11, 2012 Apr 11, 2012

Copy link to clipboard

Copied

Hi there,

I'm loading a bitmap with FileReference.load() and than dump the byteArray into a Loader object and than add that loader object to stage.

The problem is that regardless if I load the loader into a Sprite or add it to the stage, neither the loader or the sprite don't have width or height. It's always 0. So I can't scale the image as I don't know it's dimensions.

Does anyone had this problem before and found a solution?

Thank you!

TOPICS
ActionScript

Views

1.1K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Apr 11, 2012 Apr 11, 2012

this code works

package

{

          import flash.display.Loader;

          import flash.display.LoaderInfo;

          import flash.display.Sprite;

          import flash.events.Event;

          import flash.events.MouseEvent;

          import flash.net.FileFilter;

          import flash.net.FileReference;

          import flash.text.TextField;

          import flash.text.TextFieldAutoSize;

          public class Main extends Sprite

          {

                    private var fileRef:FileReference;

            

...

Votes

Translate

Translate
Advocate ,
Apr 11, 2012 Apr 11, 2012

Copy link to clipboard

Copied

this code works

package

{

          import flash.display.Loader;

          import flash.display.LoaderInfo;

          import flash.display.Sprite;

          import flash.events.Event;

          import flash.events.MouseEvent;

          import flash.net.FileFilter;

          import flash.net.FileReference;

          import flash.text.TextField;

          import flash.text.TextFieldAutoSize;

          public class Main extends Sprite

          {

                    private var fileRef:FileReference;

                    public function Main():void

                    {

                              if (stage)

                              {

                                        init();

                              }

                              else

                              {

                                        addEventListener(Event.ADDED_TO_STAGE, init);

                              }

                    }

                    public function init(e:Event = null):void

                    {

                              removeEventListener(Event.ADDED_TO_STAGE, init);

                              fileRef = new FileReference();

                              stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);

                              var txt:TextField = new TextField();

                              addChild(txt);

                              txt.autoSize = TextFieldAutoSize.LEFT;

                              txt.x = txt.y = 20;

                              txt.text = "click anywhere to load an image file...";

                    }

                    private function onDown(evt:MouseEvent):void{

                              fileRef.browse([new FileFilter("Images", "*.jpg;*.gif;*.png")]);

                              fileRef.addEventListener(Event.SELECT, onSelected);

                              stage.removeEventListener(MouseEvent.MOUSE_DOWN, onDown);

                    }

                    private function onSelected(evt:Event):void{

                              fileRef.addEventListener(Event.COMPLETE, onLoaded);

                              fileRef.load();

                              fileRef.removeEventListener(Event.SELECT, onSelected);

                    }

                    private function onLoaded(evt:Event):void{

                              var loader:Loader = new Loader();

                              loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);

                              loader.loadBytes(evt.target.data);

                              addChild(loader);

                              fileRef.removeEventListener(Event.COMPLETE, onLoaded);

                    }

                    private function onComplete(e:Event):void

                    {

                              var loaderInfo:LoaderInfo = e.target as LoaderInfo;

                              loaderInfo.removeEventListener(Event.COMPLETE, onComplete);

                              var loader:Loader = loaderInfo.loader;

                              trace("loader size: ", loader.width, loader.height);

                    }

          }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 11, 2012 Apr 11, 2012

Copy link to clipboard

Copied

Hi,

Thank you.

But I know that method works, that's the method to load the image. The problem is that after you load it, it has 0 (zero) width and height even though the image displays. So I can't manipulate it as I can't find it's width and height.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 11, 2012 Apr 11, 2012

Copy link to clipboard

Copied

sorry i initially left a bit off and have since updated the code.

if you try and get the width and height of loader just after you call loadBytes you will get 0, 0

you have to setup and eventlistener for Event.COMPLETE and when you receive that you can access the width and height

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Apr 11, 2012 Apr 11, 2012

Copy link to clipboard

Copied

I see.

I didn't though that loadBytes was actually a loading process which of courses needs an onComplete event. It though it just dumps the bytes into the loader object since the image is already loaded into RAM.

Tnx mate!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Apr 11, 2012 Apr 11, 2012

Copy link to clipboard

Copied

LATEST

no worries

the file reference loads the bytes into a ByteArray. Loader then needs to load the bytes from the ByteArray. It will be pretty quick but you still have to wait

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines