# ImageLoader bug

**URL:** <https://forum.kirupa.com/t/imageloader-bug/317604>\
**Category:** flash\
**Created:** [December 27, 2010, 11:05am UTC](https://forum.kirupa.com/t/imageloader-bug/317604 "2010-12-27T11:05:36Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![spectator](https://avatars.discourse-cdn.com/v4/letter/s/47e85d/32.png) [@spectator](https://forum.kirupa.com/u/spectator)\
**Post date:** [December 27, 2010, 11:05am UTC](https://forum.kirupa.com/t/imageloader-bug/317604/1 "2010-12-27T11:05:36Z")

</div>

Hello :). I wrote one class which purpose is to load image into to certain MovieClip. The problem is that it is not working very well.

```php
package src.load {
    import flash.display.MovieClip;
    import flash.display.Loader;
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import flash.net.URLRequest;

    public class ImageLoader extends MovieClip {
        
        private var _holder:MovieClip;
        private var _holderWidth:Number;
        private var _holderHeight:Number;

        public function ImageLoader() {
        }
        public function loadImage(clip:String, holder:MovieClip, holderWidth:Number, holderHeight:Number):void {
            trace("PASSED CLIP");
            trace(clip);
            removeChildren(holder);

            var imageLoader:Loader = new Loader();
            var imageURL:String = clip;
            var imageURLReq:URLRequest = new URLRequest(imageURL);
            
            trace("loading "+imageURL);

            imageLoader.load(imageURLReq);
            imageLoader.contentLoaderInfo.addEventListener(Event.INIT, imageLoaded);
            imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageProgress);
            
            _holder = holder;
            _holderWidth = holderWidth;
            _holderHeight = holderHeight;
        }

        private function imageProgress(evt:ProgressEvent):void {
            trace(Math.round((evt.bytesLoaded / evt.bytesTotal)*100));
        }

        private function imageLoaded(evt:Event):void {
            _holder.addChild(evt.target.content);
            _holder.width = _holderWidth;
            _holder.height = _holderHeight;
        }

        private function removeChildren(container:MovieClip):void {
            while (container.numChildren) {
                container.removeChildAt(0);
            }
        }
    }
}

```

This is the class.

How am I using it?  
I listen for MouseDown and then MouseMove and I draw rectangle. After MouseUp I finish the rectangle and save it in MovieClip, add the MovieClip to the stage and then add the MovieClip to an Array. In this array I have other rectangles created the way described above. I have one variable called

> var selectedDrawing:\*; selectedDrawing = drawingsList[id];  
> which holds the current item of the array being viewed. The problem appears here:

```auto
_imageLoader.loadImage(imgData[1], selectedDrawing, selectedDrawing.width, selectedDrawing.height);

```

The image is not loaded in the selectedDrawing it is not centered to it. What should I do just to load random image to Rectangle created with this method:

```auto
graphics.beginFill();
graphics.drawRect(...);

```

If the image is larger than the container then I should reduce its width and height to the selectedDrawing. But the code above seems not to be working well.

Where is the problem? I am sure you have come around such things. Please give me a hint how to solve this puzzle 🙂

Thank you in advance.
