Search code examples
actionscript-3image-loadingurlrequestrobotlegs

Load images using loader class and send the images in an arrayCollection


I am facing a particular problem which I am unable to resolve. I am using ActionScript 3 with Robotlegs framework. I have to make a series of URL Requests in a command. Each URL gives an image. I am pushing these images into an ArrayCollection and then dispatching an event with this ArrayCollection as the payload. This ArrayCollection is sent to a view which uses an itemRenderer to display the images. I have checked that images are being loaded to the ArrayCollection but the images are not being displayed in the view.

This is what I have written in the command,

var arrayCollect:ArrayCollection = new ArrayCollection();
var my_loader:Loader = new Loader();

my_loader.load( new URLRequest( "http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg" ) );
arrayCollect.addItem(my_loader as Object );
my_loader.load( new URLRequest( "http://www.joomlaworks.net/images/demos/galleries/abstract/8.jpg" ) );
arrayCollect.addItem(my_loader as Object );

return arrayCollect;

Then the arrayCollect is being dispatched in a event as payload.

dispatch( new XYZEvent ( XYZEvent.LOAD_COMPLETE, arrayCollect ) );

In the view, I used an itemRenderer.

<s:List id="pqr"
dataProvider="{data}">

....
</s:List>

The images are not being displayed. I think the problem is that the data in the arrayCollection are objects of type 'loader'. So I tried to typecast it as a BitmapImage, but it did not resolve the issue.

Please help me resolve this.


Solution

  • Your problem is you didn't define a item renderer which could show your data. See http://help.adobe.com/en_US/Flex/4.0/UsingSDK/WS03d33b8076db57b9-23c04461124bbeca597-8000.html for creating custum itemrenderers.

    Next: in your ArrayCollection you didn't create a new Loader. Meaning the ArrayCollection item 1 is pointing to the same laoder is item 2.

    I added an example with 2 lists, one without a renderer an one with a custum renderer. I didn't use the loaders, but of course you could create a renderer which add a laoder to teh display instead of using a Flex image component.

    Main application:

    <?xml version="1.0" encoding="utf-8"?>
    <s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
                       xmlns:s="library://ns.adobe.com/flex/spark" 
                       xmlns:mx="library://ns.adobe.com/flex/mx">
    <fx:Declarations>
        <!-- Place non-visual elements (e.g., services, value objects) here -->
    </fx:Declarations>
    
    <fx:Script>
        <![CDATA[
            import mx.collections.ArrayCollection;
    
            private var _dataProviderList : ArrayCollection = new ArrayCollection(
                [
                    "http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg",
                    "http://www.joomlaworks.net/images/demos/galleries/abstract/8.jpg"
                ]
            );
    
        ]]>
    </fx:Script>
    
    <s:layout>
        <s:VerticalLayout/>
    </s:layout> 
    
    <s:List dataProvider="{_dataProviderList}">
    
    </s:List>
    
    <s:List itemRenderer="ImageItemRenderer" dataProvider="{_dataProviderList}">
    
    </s:List>
    
    </s:WindowedApplication>
    

    Renderer

    <?xml version="1.0" encoding="utf-8"?>
    <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" 
                autoDrawBackground="true">
    
    
    <mx:Image 
              source="{data}" 
              width="50" height="50"/>
    
    </s:ItemRenderer>
    

    If you realy want to use a laoder as object in your ArrayCollection, you could write your renderer as:

    <?xml version="1.0" encoding="utf-8"?>
    <s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009" 
                xmlns:s="library://ns.adobe.com/flex/spark" 
                xmlns:mx="library://ns.adobe.com/flex/mx" 
                autoDrawBackground="true">
    
    <fx:Script>
        <![CDATA[
    
            [Bindable]
            private var _loader : Loader;
    
            private function dataLoaded (evt : Event) : void
            {
                _loader = (data as Loader) 
            }
    
            override public function set data(value:Object):void
            {
                super.data = value;
    
                if (data is Loader) {
                    // render items may be reuses (http://stackoverflow.com/questions/7328989/itemrenderer-switching-urlloader-images)
                    (data as Loader).contentLoaderInfo.removeEventListener(Event.COMPLETE, dataLoaded);
    
                    // data allready loaded
                    if ((data as Loader).content) {
                        _loader = (data as Loader)
                    }
                    else {
                        // wait till data is loaded
                        (data as Loader).contentLoaderInfo.addEventListener(Event.COMPLETE,dataLoaded);
                    }
                }
            }
        ]]>
    </fx:Script>
    
    <mx:Image 
        source="{_loader}" 
        width="50" height="50"/>
    
    
    
    </s:ItemRenderer>