A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Image loading problem

  1. #1
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766

    [solved]Image loading problem

    Code:
    private function getImage():void
                {
                    fr.addEventListener(Event.SELECT,imgLoad);
                    fr.addEventListener(Event.COMPLETE,imgExtract);
                    fr.browse([new FileFilter("Images(*.png,*.jpg,*.bmp)","*.png;*.jpg;*.jpeg;*.bmp")]);
                }           
                private function imgLoad(e:Event):void
                {
                    fr.load();
                }
                private function imgExtract(e:Event):void
                {
                    var img_br:ByteArray = new ByteArray();
                    img_br = fr.data as ByteArray;
                   
                    var loader:Loader = new Loader();
                    loader.addEventListener(Event.COMPLETE,imgDisplay);
                    loader.addEventListener(IOErrorEvent.IO_ERROR,imgLoading);
                    loader.loadBytes(img_br);
                    pathtxt.text = "Loading "+img_br.length+" : "+loader;
                }
                private function imgLoading(e:IOErrorEvent):void
                {
                    pathtxt.text = "here";
                    //imgProgress.setProgress(e.bytesLoaded,e.bytesTotal);
                }
                private function imgDisplay(e:Event):void
                {
                    image = e.currentTarget.loader.content as Bitmap;
                    imgPreview.addChild(image);
                    pathtxt.text = fr.name;               
                }

    When I run the above, it doesn't seem to trigger the imgDisplay or imgLoading functions(imgLoading was supposed to be progress event, but I changed it for testing).....it seems, the eventListeners are not working, any idea?
    Last edited by bluemagica; 10-11-2009 at 12:30 AM. Reason: Topic solved by myself. Solution: forgot to use contentLoaderInfo
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    What is fr? File Reference?
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  3. #3
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    When I did this in my current application, I had to take a step back and research because personally I think I'm the last known developer still using the stock Flash CS4 IDE instead of Flex. I came across more than one article talking about how bug-ridden FileReference.load() was so I took a different approach. Use a File.browse() to get your local path, and then revert to a URLRequest to load the actual image. I can't show you a work example, but I can pull bits and pieces from my current project:

    Code:
    		private function loadLocalPrev(event:MouseEvent) {
    			var localRef:File;
    			localRef = new File();
    			localRef.addEventListener(Event.SELECT, selectHandler);
    			localRef.browse([new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png")]);
    		}
    Code:
            private function selectHandler(event:Event):void {
    			trace("Loading image: " + event.target.nativePath);			
                if (event.target.nativePath != null) {
    				loadSinglePrev( event.target.nativePath);
    			} else {
    				new Alert(_stage, _newMoviePic.x, _newMoviePic.y, _newMoviePic.width, _newMoviePic.height, false, false, "Data Required", "You must select a file.");
    			}
            }
    Code:
    		private function loadSinglePrev(url:String) {
    			trace("Loading image: " + url);
    			for (var i = 0; i< _newMoviePic._previews.length; i++) {
    				_newMoviePic.removeChild(_newMoviePic._previews[i].img);
    			}
    			_newMoviePic._previews = [];
    			var img:Image = new Image(_newMoviePic, 126, 195, false);
    			img.loadImage(url, doNothing);
    			img._id = 0;
    			img.x = 105;
    			img.y = 172;
    			_newMoviePic.addChild(img);
    			var o:Object = {};
    			o.img = img;
    			o.id = 0;
    			o.url = url;
    			_newMoviePic._previews.push(o);
    			img.doubleClickEnabled=true;
    			img.addEventListener(MouseEvent.CLICK, selectPreview);
    			img.addEventListener(MouseEvent.DOUBLE_CLICK, downloadAndfinalizeAdd);
    			
    		}
    Code:
    // this is from the Image class' loadImage() function
    		public function loadImage(url:String, callback:Function = null) {
    			var urlGet = new URLRequest(url);
    			_imageloader.mouseEnabled=false;
    			configureListeners(_imageloader.contentLoaderInfo);
    			_imageloader.load(urlGet);
    			this.addChild(_imageloader);
    			if (_showProg) {
    				this.addChild(_imageProgress);
    			}
    			_callBack = callback;
    		}
    Hopefully you can get the idea from there. Maybe it will help you, maybe it won't.

    Nice Maplestory avatar by the way; been meaning to mention that for a while now.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  4. #4
    Senior Member bluemagica's Avatar
    Join Date
    Jun 2008
    Posts
    766
    umm, there is no nativePath. I don't know about air, but this is a web-app, and it seems I have no way to find the local path to the file from file reference.
    http://www.adobe.com/devnet/flash/qu...nce_class_as3/
    There it is said: "when the user has selected a file to open, there is no way to determine the local path where the file is located. Only the name of the file is available."
    if I could get the path some way, then I think your solution will work!
    And thanks, I like the avatar very much.....it matches me perfectly, lol!

    And yes, fr is filereference!




    I don't think the filereference is the problem though, cause as you can see from the path text line(well, you can't see, but assume), the file is being loaded, and I am getting the bytearray, so I guess parsing that bytearray through loadBytes is causing the problem somehow!
    If you like me, add me to your friends list .

    PS: looking for spriters and graphics artists for a RPG and an Arcade fighting project. If you can help out, please pm me!

    My Arcade My Blog

    Add me on twitter:

  5. #5
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,377
    Ahh. Yet another unfortunate side-effect of pushing Flex/AIR into the same forum. You're using Flex on the web, and I'm using AIR locally.
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center