English 中文(简体)
在Adobe Air Flex4.5中将app:/images加载到HTMLLoader中?
原标题:Load app:/ images into HTMLLoader in Adobe Air Flex4.5?

我正在用AdobeAIR构建一个应用程序,其中包含一个<code>mx:HTML</code>的浏览组件。此浏览器将用于加载外部内容(可公开访问的URL,如http://google.com),在外部内容加载后,我将CSS和JS注入HTMLLoader,为我的应用程序提供其他UI组件。

那部分工作得很好。我的JS代码由WebKit执行;WebKit使用我的CSS规则。对于我使用CSS和JS创建的UI组件,我希望使用一些图像。到目前为止,我已经通过数据URI方案,但这迫使我在资产/文件夹中制作图像的base64编码副本(添加到CSS规则中)。

我希望能够用HTMLLoader加载外部URL,然后添加CSS、JS和Images,在同一个HTMLLoader中创建UI组件。我知道HTMLLoader属性placeLoadStringContentInApplicationSandbox,但如果这是我将用于将本地图像从我的资产/加载到HTMLLoader中的,IMG标记的HTML内容会是什么样子,即如何引用图像?

关于如何将本地图像加载到HTMLLoader对象的内容中,还有其他想法吗?

更新

我尝试了第一个评论员的想法,使用src=“app://path/to/image.png”,但没有成功。

以下是我所做的:

<mx:HTML id="browser"/> 
<fx:Script>
<![CDATA[
browser.addEventListener(Event.COMPLETE,browserEventCompleteHandler);    
browser.htmlLoader.placeLoadStringContentInApplicationSandbox = true; // have set to true and false with no difference.
....initiate browser load....
private function browserEventCompleteHandler(event:Event):void 
{
    var img:* = browser.domWindow.document.createElement( IMG );
    img.src = "app:/assets/arrow_refresh.png";
    img.width="300";
    img.height="300";
    browser.domWindow.document.getElementsByTagName( body )[0].appendChild(img);
}
]]>
</fx:Script>

运行上面的代码,我看到IMG元素被添加到了宽度和高度为300px的外部内容中,但PNG图像本身并不呈现。只需为IMG元素看到一个300像素的空正方形。

最终更新

The answer solved my problem, but with one change. I used File and FileStream classes instead of URLRequest to load the png. 以下是我所做的:

var f:File = File.applicationDirectory.resolvePath("app:/assets/arrow_refresh.png");
var s:FileStream = new FileStream();
s.open(f, flash.filesystem.FileMode.READ);
var data:ByteArray = new ByteArray();
s.readBytes(data,0,s.bytesAvailable);
var b64Encoder : Base64Encoder = new Base64Encoder;
b64Encoder.encodeBytes(data);
最佳回答

为什么不在运行时在base64中对img进行编码?

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.html.HTMLLoader;
    import flash.net.URLLoader;
    import flash.net.URLLoaderDataFormat;
    import flash.net.URLRequest;

    // Base64Encoder from as3corelib (https://github.com/mikechambers/as3corelib)
    import mx.utils.Base64Encoder;

    public class TestHtml extends Sprite
    {
        private var _htmlLoader : HTMLLoader = new HTMLLoader();

        private var _rawImgLoader : URLLoader = new URLLoader; 

        public function TestHtml()
        {
            super();

            _htmlLoader.width = stage.stageWidth;
            _htmlLoader.height = stage.stageHeight;

            // Load google page
            _htmlLoader.load(new URLRequest("http://www.google.fr"));
            _htmlLoader.addEventListener(Event.COMPLETE,   onEvent);

            // Load one image
            _rawImgLoader.dataFormat = URLLoaderDataFormat.BINARY;
            _rawImgLoader.addEventListener(Event.COMPLETE, onEvent);
            _rawImgLoader.load(new URLRequest("logo.png"));

            addChild(_htmlLoader);
        }

        private function onEvent(e:Event) : void
        {
            addImg();
        }

        private function addImg() : void
        {
            // Wait for img and html
            if(_htmlLoader.loaded && _rawImgLoader.data)
            {
                // Encode img
                var b64Encoder : Base64Encoder = new Base64Encoder;
                b64Encoder.encodeBytes(_rawImgLoader.data);

                var img:Object = _htmlLoader.window.document.createElement( IMG );
                img.src = "data:image/png;base64," + b64Encoder.flush();
                img.width="300";
                img.height="300";
                var body :Object = _htmlLoader.window.document.getElementsByTagName( div );
                body[0].appendChild(img);
            }
        }
    }
}
问题回答

placeLoadStringContentInApplicationSandbox不起作用,因为您的内容不是从字符串加载的,而是从外部url加载的。并且远程内容被限制使用本地文件。如果加载html并手动将其设置为htmlText属性,它将成为本地内容,但这可能会出现问题(如页面闪烁)





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Multiple Remote call made simultenously

I was making multiple remote calls and they are done sequentially and when I am getting a result event back it s triggering calls to all the methods with ResultEvent as an argument . I am supposed to ...

Attaching a property to an event in Flex/AS3

I have a parameter that needs to be passed along with an event. After unsuccessful attempts to place it on the type by extending the class, I ve been advised in another SO question to write a custom ...

Clearing RSL in Cache

I have built a flex application which has a "main" project and it is assosciated with a few RSL s which are loaded and cached once i run my "main" application. The problem i am facing is that the ...

What s a good way of deserializing data into mock objects?

I m writing a mock backend service for my flex application. Because I will likely need to add/edit/modify the mock data over time, I d prefer not to generate the data in code like this: var mockData =...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

热门标签