views:

1089

answers:

3

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?

+1  A: 

Not sure but the problem you have seems to be symptomatic of a cross-domain issue. You will not be able to modify a loaded SWFs properties (smoothing in this case) if it is originating from a different domain unless a cross-domain policy file permits it.

If that's not the problem, I remember radically drawing the bitmapData always did the trick. If the result is a black image then you are mostly sure having a cross-domain problem. This article explains the technique precisely for AS2 :

http://www.kaourantin.net/2005/12/dynamically-loading-bitmaps-with.html

Theo.T
Crossdomain is not a problem, since the server I'm talking to has a crossdomain.xml file. The problem with that code is that I can't get it to resize my images, like i do in my code. Any ideas how I would use that code to also resize the iamges?
mofle
+1  A: 

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!

Ypmits
You can do that in the function 'doneLoading'. i.e.: public function doneLoading(mc : MovieClip) : Void { mc._width = 160; mc._yscale = mc._xscale; if( target._height > 105) { mc._height = 105; mc._xscale = target._yscale; } }
Ypmits
+1  A: 

How about drawing it on another BitmapData with a scaling matrix? I heard it will be smoother than scaling the Bitmap itself...

The code below is from here.

// source in this example is a DisplayObject
var temp:BitmapData = new BitmapData( sourceWidth, sourceHeight );
temp.draw( source );

var output:BitmapData = new BitmapData( outputWidth, outputHeight );

var matrix:Matrix = new Matrix();
matrix.scale( outputWidth / sourceWidth, outputHeight / sourceHeight );

output.draw( temp, matrix, null, null, null, true );
temp.dispose();
Andy Li