简体   繁体   中英

Flex app not displaying images sometimes loaded from a URL

I have the following Actionscript code in my Flex 4 app to display images on a Sprite. Works perfect in the local flash player while developing this:

private function initializePhoto():void {
    var photoLoader:Loader = new Loader();
    photoLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onPhotoReady);
    photoLoader.load(new URLRequest("http://image.mydomain.com/icon/mark.png"));
}

public function onPhotoReady(event:Event):void {
    var marker:Bitmap = new Bitmap();
    marker.x = -32;
    marker.y = -8;
    marker.smoothing = true;
    marker.bitmapData = event.target.content.bitmapData;
    marker.width = 64;
    marker.height = 64;
    addChild(marker);
}

So I take the resulting .swf file and run it in my local webserver. Looks good. Upload it to staging and looks good. The next day on staging, the image doesn't show up! Confused, I look at firebug and it shows the image is in fact being downloaded... just not visible. I look at firebug locally when viewing the app on my local webserver and the image is only sometimes showing up! But always it's downloading the image. It never fails when in dev mode through the adobe flash player.

What's going on? I guess I'm a bit confused... My thoughts are:

  • Some kind of security issue that I didn't have before. Would explain why it always works on the standalone adobe flash player and maybe the reason I saw the images at first was that they were cached?
  • Race condition, possible? Would explain why the adobe flash player works, because it's instant.. while the local and especially staging servers have a tiny lag. Maybe they happened to have been particularly fast when I first tested?

My guess is there's some kind of race condition, do I have to wait after the onPhotoReady method is called before adding the Bitmap to the Sprite?

UPDATE : This is very frustrating to have so much trouble with such a simple operation (displaying an image from a URL). So when this does fail, what's happening is the Event.COMPLETE event is never fired. However, the ProgressEvent.PROGRESS is always fired twice (first with 0 bytesLoaded and then with all bytesLoaded). No errors are ever thrown or fired.

Interestingly, when this does work, I see in Firebug that http://image.mydomain.com/crossdomain.xml is requested, while that is not requested when this does not work (even though ProgressEvent.PROGRESS callback shows all bytes are loaded). I would've imagined that crossdomain.xml would be called before getting the image.

UPDATE 2 : Fixed by simply adding the extra parameter new LoaderContext(true) to the photoLoader.load() call.

Did you check if this could be a cross domain policy issue?? If the issue does not happen consistently then it cannot be a security issue.

Incase you do not need to edit the bitmap data, you can try simply taking and image object, and giving the url as the source. Till now that has worked for me , without fail.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM