Search code examples
flashimageactionscriptactionscript-2smoothing

Image scaling and smoothing


I'm importing some images dynamically into a SWF from an external site using AS2. It works perfectly when I load my images from my computer, but when I try to load them from the external server the smoothing doesn't work.

My code:

    var imageLoad:MovieClipLoader = new MovieClipLoader();
    imageLoad.addListener({
        onLoadInit:function (target:MovieClip) {
            target._quality = "BEST";
            target._width = 160;
            target._yscale = target._xscale;
            if (target._height>105) {
                target._height = 105;
                target._xscale = target._yscale;
            }
            target.forceSmoothing = true;
        }
    });
imageLoad.loadClip(imageURL,imageMC);

I have tried out every solution I could find on the net, and no one worked with smoothing...

Any solution to this?


Solution

  • AS2... ah... the memories (more like nightmares).

    Try out my 'good-old' BitmapLoader.as... I've used it for many many years and has never failed me yet... It's not beautifully written and there are some double scope setters in there... but I don't care. It's old, and it has done it's job perfectly (always!). It needs a Boolean in the constructor which sets the smoothing to true or false

    import flash.display.BitmapData;
    
    class BitmapLoader extends Object {
    
        private var mLoader : MovieClipLoader;
        private var scope : Object;
        private var _mc : MovieClip;
        private var _url : String;
        private var _func : Object;
        private var smooth : Boolean;
    
        public function BitmapLoader(smooth : Boolean) 
        {
            this.smooth = smooth;
            mLoader = new MovieClipLoader( );
            addListener( this );    
        }
    
        public function addListener(inListener : Object) : Void 
        {
            mLoader.addListener( inListener );
            scope = inListener;
        }
    
        public function removeListener(inListener : Object) : Void 
        {
            mLoader.removeListener( inListener );
        }
    
        private function onLoadInit(inTarget : MovieClip) : Void 
        {
            var bitmap : BitmapData = new BitmapData( inTarget._width, inTarget._height, true, 0x000000 );      
            bitmap.draw( inTarget );
            var parent : MovieClip = inTarget._parent;
            var img : MovieClip = parent.createEmptyMovieClip( "imageloader_smooth_mc", parent.getNextHighestDepth( ) );
            inTarget.unloadMovie( );
            inTarget.removeMovieClip( );
            delete inTarget;
            img.attachBitmap( bitmap, img.getNextHighestDepth( ), "never", true );
            scope[_func]( img );
        }
    
        private function onLoadError(errorCode : String, httpStatus : Number) : Void 
        {
            error( errorCode, httpStatus );
        }
    
        /**
         * loadBitmap( http://www.test.nl/img.jpg, movieclip, "dothis");
         */
        public function loadBitmap(url : String, mc : MovieClip, func : Object) : Void 
        {
            _url = url;
            _mc = mc;
            _func = func;
            var raw : MovieClip = _mc.createEmptyMovieClip( "imageloader_raw_mc", _mc.getNextHighestDepth( ) );
            mLoader.loadClip( _url, raw );
        }
    
        private function error(errorCode : String, httpStatus : Number) : Void 
        {
            var raw : MovieClip = _mc.createEmptyMovieClip( "imageloader_raw_mc", _mc.getNextHighestDepth( ) );
            mLoader.loadClip( "img/notfound.jpg", raw );
        };
    }
    

    You can use this class like this:

        var loader : BitmapLoader = new BitmapLoader( true );
    loader.addListener( this );
    loader.loadBitmap( "http://test.nl/example.jpg", this, "doneLoading" );
    

    'true' is the smoothing-boolean, addListener( this ) is to prevent scope-problems (AS2-bleeh) and "doneLoading" is the function name it calls when it's done loading.

    Hope this works for ya.

    Good luck!