How to get smooth image scaling in Flex

snipped by vixiom

Sometimes images loaded externally into a Flex app will become pixelated when scaled, here's how to smooth them and make sure cross domain images load properly.

Step 1: The smooth Image class written by Ben Longoria

package
{
    import mx.controls.Image;
    import flash.display.Loader;
    import flash.display.Bitmap;
    import flash.events.Event;
    import mx.core.mx_internal;
 
    use namespace mx_internal;
 
    /**
     * SmoothImage
     *
     * Automatically turns smoothing on after image has loaded
     *
     * @author Ben Longoria
     */
    public class SmoothImage extends Image {
 
        public function SmoothImage():void {
            super();
        }
 
        /**
         * @private
         */
        override mx_internal function contentLoaderInfo_completeEventHandler(event:Event):void {
            var smoothLoader:Loader = event.target.loader as Loader;
            var smoothImage:Bitmap = smoothLoader.content as Bitmap;
            smoothImage.smoothing = true;
 
            super.contentLoaderInfo_completeEventHandler(event);
        }
    }
}

Step 2: Set the image's loaderContext to avoid any cross-domain issues.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" xmlns:local="*" initialize="init();">
 
	<mx:Script>
	    <![CDATA[
	    import flash.system.Security;
 
	    [Bindable]
	    public var loaderContext:LoaderContext;
 
	    public function init():void
            {
                loaderContext = new LoaderContext();
                loaderContext.checkPolicyFile = true;
            }
 
	    ]]>
	</mx:Script>
 
	<local:SmoothImage width="640" height="480" id="image" loaderContext="{loaderContext}" />
 
</mx:Application>