How do I set the source s: BitmapFill dynamically? (Flash Builder, code inside)

In Flash Builder (flex 4) I am trying to use the following code to set the user (from the file system) an image as a repeating background. It worked with mx: Image, but I want to use the cool repap capabiletis s: BitmapFill.

BTW: The Technic I'm using doesn't work with S: BitmapImage either. Also FP doesn't return any errors. What should I do with my code to make it work?

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
               xmlns:s="library://ns.adobe.com/flex/spark" 
               xmlns:mx="library://ns.adobe.com/flex/mx"
               xmlns:net="flash.net.*"
               minWidth="955" minHeight="600" >

    <fx:Script>
        <![CDATA[
            import mx.controls.Alert;
            import mx.utils.ObjectUtil;

            private function btn_click(evt:MouseEvent):void {
                var arr:Array = [];
                arr.push(new FileFilter("Images", ".gif;*.jpeg;*.jpg;*.png"));
                fileReference.browse(arr);
            }

            private function fileReference_select(evt:Event):void {
                fileReference.load();
            }

            private function fileReference_complete(evt:Event):void {
                img.source = fileReference.data;
                Alert.show(ObjectUtil.toString(fileReference));
            }
        ]]>
    </fx:Script>

    <fx:Declarations>
        <net:FileReference id="fileReference"
                           select="fileReference_select(event);"
                           complete="fileReference_complete(event);" />
    </fx:Declarations>

<s:Rect id="backgroundRect" left="0" right="0" top="0" bottom="0">
    <s:fill>
        <s:BitmapFill id="img" source="@Embed('1.jpg')" fillMode="repeat" />
    </s:fill>
</s:Rect>
<mx:Button id="btn"
                       label="Browse and preview..."
                       click="btn_click(event);" />
</s:Application>

      

+2


source to share


1 answer


Check out this code:

        private function fileReference_complete(evt:Event):void {           
            var loader:Loader = new Loader();
            loader.loadBytes(fileReference.data);
            loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loaderCompleteHandler);
        }

        private function loaderCompleteHandler(evt:Event):void{
            img.source = evt.target.content;
        }

      



Thus, you need to use a loader to create the bitmap object.

+4


source







All Articles