mardi 29 septembre 2015

Flex mx Image source path at runtime

I'm wading through a Flex AIR desktop project that someone else wrote. The original author has used several mx.controls.Image components. Runtime image paths assigned like this:

image.source = "/assets/book.png";

It doesn't work - I just get the broken image icon.

I've never used the above approach in my own code. Personally, I've always used compile-time embedded images or URLLoader/Loader for runtime images.

So, I'd like to learn how to get this image path approach working.

I wrote a simple test program. Here is my .mxml -

<?xml version="1.0" encoding="utf-8"?>
 <pf:LearningAS xmlns:fx=""
      <mx:Image id="myImage"/>

Here is my connected .as

public class LearningAS extends WindowedApplication {

    public var myImage:Image;

    public function LearningAS() {
        addEventListener(FlexEvent.CREATION_COMPLETE, init);

    protected function init(event:FlexEvent):void {
        myImage.source = '/assets/myimage.png';

I also added the src/assets folder to AIR package contents. And I added -use-network=false to my compiler directives. (I'm using FDT, and Flex 4.6).

Aucun commentaire:

Enregistrer un commentaire