【问题标题】:Embeding images in an Flex application在 Flex 应用程序中嵌入图像
【发布时间】:2012-03-02 00:10:44
【问题描述】:

我使用工具提示中的图像。图像在服务器上。我正在使用代码:

    var tip1:String;
    tip1 = "<img src='assets/images/yes.jpg' align='center' width='150' height='150' hspace='3' vspace='3'/>";
tip1 +=  'some text';        
yes.toolTip = tip1;

但是很多图片超过 100 kb,那么工具提示中的图片会出现一些延迟。是否可以在加载 swf 的过程中嵌入所有图片,在鼠标悬停时与文本一起出现?

【问题讨论】:

    标签: apache-flex actionscript-3


    【解决方案1】:

    确实如此。添加您想要包含在 Flex 应用程序中的图像,然后将它们嵌入到您的代码中,如下所示:

    <fx:Script>
      <![CDATA[
    
        [Embed(source="assets/images/yes.jpg")]
        [Bindable]
        public var YesIcon:Class;
    
    ]]>
        </fx:Script>
    
    <mx:Image source="{YesIcon}" />
    

    如果你真的想在工具提示中使用它,这里有一篇关于如何做到这一点的好文章:http://blog.flexmp.com/2008/09/10/flex-custom-tooltip-speech-bubble/

    编辑: 这是一个快速而肮脏的示例,说明如何在应用程序启动时将图像预加载到 ArrayCollection 中。您需要添加一些代码以确保在启用应用程序或执行其他操作之前加载所有图像,但这应该可以帮助您开始。

    <?xml version="1.0" encoding="utf-8"?>
    <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009" 
             xmlns:s="library://ns.adobe.com/flex/spark" 
             xmlns:mx="library://ns.adobe.com/flex/mx"
             creationComplete="creationCompleteHandler(event)">
        <fx:Script>
            <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.events.FlexEvent;
    
                private var imageArray:ArrayCollection = new ArrayCollection();
                private var imageArrayIndex:int = 0;
                private var imagesToLoad:ArrayCollection = new ArrayCollection();
    
                protected function creationCompleteHandler(event:FlexEvent):void
                {
                    // Load your XML into the "imagesToLoad" ArrayCollection. 
                    // This should contain your list of images we need to load.
    
                    PreloadImages();
                }
    
                protected function PreloadImages():void
                {
                    var request:URLRequest = new URLRequest(imageArray[imageArrayIndex]);
                    var imageLoader:Loader = new Loader();
                    var loaderContext:LoaderContext = new LoaderContext();
    
                    loaderContext.checkPolicyFile = true;
                    imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, PreloadImage_CompleteHandler);
                    imageLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, PreloadImage_ErrorHandler);
                    imageLoader.load(request,loaderContext);
                }
    
                // Called when the Loader we declared in PreloadImages() is done loading the image.
                protected function PreloadImage_CompleteHandler(event:Event):void
                {
                    imageArray[imageArrayIndex] = new Bitmap(Bitmap(event.currentTarget.content).bitmapData);
    
                    // Check to see if there's still images that need to be loaded.
                    if (imageArrayIndex < imagesToLoad.length - 1)
                    {
                        imageArrayIndex = imageArrayIndex + 1;
                        PreloadImages();
                    }
                }
    
                // Called when the Loader we declared in PreloadImages() encountered an error trying to load the image.
                protected function PreloadImage_ErrorHandler(event:Event):void
                {
    
                    Alert.show(imageArray[imageArrayIndex].toString() + " could not be loaded.\n\nPlease make sure the file exists.");
    
                    // Check to see if there's still images that need to be loaded.
                    if (imageArrayIndex < imageArray.length - 1)
                    {
                        imageArrayIndex = imageArrayIndex + 1;
                        PreloadImages();
                    }
                }
    
            ]]>
        </fx:Script>
    </s:Group>
    

    您可能想要查看的另一个不错的组件是 Arthur Debert 创建的 Flex BulkLoader。它也可以很好地满足您的需求。

    https://github.com/arthur-debert/BulkLoader

    【讨论】:

    • 嗨!谢谢。很有用的文章。但我使用大约 50-70 张照片。我有一个源代码是从 XML 加载的。我必须为每张图片创建一个变量吗?
    • 如果总是相同的 50-70 张图片,那么可以,我会为每张图片创建一个变量,以便它们嵌入到您的应用中。如果每次运行应用程序时图像都可能发生变化,我会在应用程序启动时循环遍历 xml 并将每个图像加载到位图对象数组中。图像不会被嵌入,但您可以使用 Array 中的 Bitmap 对象作为图像的来源,它应该可以消除延迟。
    • 谢谢。的确,我不太明白如何在后台实现这样的加载每个图像。
    • 我已经用一个快速示例来编辑我的答案,说明如何将图像预加载到 ArrayCollection 中。希望这对您有所帮助。 :)
    • 非常感谢。你真的帮了我很多。不幸的是,我在行 var request:URLRequest = new URLRequest(imageArray[imageArrayIndex]); 中有错误(RangeError:提供的索引超出范围)。可能是因为 ArrayCollection imageArray 是新的并且没有零索引元素?
    【解决方案2】:

    不能添加评论..但是改变:

    imageArray[imageArrayIndex] = new Bitmap(Bitmap(event.currentTarget.content).bitmapData);
    

    到:

    imageArray.addItem(new Bitmap(Bitmap(event.currentTarget.content).bitmapData));
    

    也注意到了:

    var request:URLRequest = new URLRequest(imageArray[imageArrayIndex].src_big);
    

    应该是:

    var request:URLRequest = new URLRequest(imagesToLoad[imageArrayIndex].src_big);
    

    杰森对此表示感谢!我对 actionscript/Flex 完全陌生,这对我有很大帮助。一个问题是.. 我怎样才能确保所有图像都已加载?我在用户交互后运行此代码,尝试加载一堆图像,一旦全部加载,我希望发生一些事情(在幻灯片中显示它们)。但不确定确保它们全部加载的最佳方法。再次感谢!

    【讨论】:

    • 嗨 toddm,我刚刚在这里看到了你的帖子。我要做的是在图像加载完成后发送一个事件。将else 语句添加到PreloadImage_CompleteHandler(event:Event) 方法中,您可以在其中检查是否有要加载的静止图像。如果您已完成加载图像(这意味着您将点击else 语句),则调度一个事件,让您的应用程序知道图像已准备好。应用程序的另一部分将侦听该事件并根据需要显示幻灯片组件。我希望这会有所帮助!
    猜你喜欢
    • 2013-07-28
    • 2011-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多