我正在使用 Adobe AIR 构建一个应用程序,其中包含一个带有 mx:HTML
的浏览组件。此浏览器将用于加载外部内容(例如,可公开访问的网址,例如 http://google.com)外部内容加载后,我将 CSS 和 JS 注入 HTMLLoader 以为我的应用程序提供其他 UI 组件。
那部分工作得很好。我的JS代码是由WebKit执行的;我的 CSS 规则由 WebKit 使用。对于我使用 CSS 和 JS 创建的 UI 组件,我希望使用一些图像。到目前为止,我已经成功地通过 数据 URI 方案 在 HTMLLoader 中渲染图像,但这迫使我在 asset/ 文件夹中制作图像的 Base64 编码副本(添加到 CSS 规则)。
我希望能够使用 HTMLLoader 加载外部 URL,然后添加 CSS、JS 和图像以在同一 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 像素正方形即可。
最终更新
答案解决了我的问题,但做了一个更改。我使用 File 和 FileStream 类而不是 URLRequest 来加载 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);
I'm building an app with Adobe AIR that contains a browsing component with mx:HTML
. This browser will be used to load external content (publicly-accessible URLs like http://google.com, for example) and after the external content loads, I'm injecting CSS and JS into the HTMLLoader to provide other UI components for my app.
That part works just fine. My JS code is executed by WebKit; my CSS rules are used by WebKit. With the UI components I'm creating with CSS and JS, I'd like to have some images used. So far, I have had success with rendering images in HTMLLoader via the data URI scheme, but this forces me to make base64-encoded copies (added to the CSS rules) of images in assets/ folder.
I'd like to be able to load external URLs with HTMLLoader, and then add CSS, JS, and Images to create UI components in the same HTMLLoader. I am aware of the HTMLLoader property, placeLoadStringContentInApplicationSandbox, but if that what I would use to load local images from my assets/ into HTMLLoader, what would the HTML content of an IMG tag look like, ie how to reference the image?
Any other ideas on how to load local images into the content of an HTMLLoader object?
UPDATE
I've tried the first commentator's idea, using src="app:/path/to/image.png" with no luck.
Here's what I did:
<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>
Running the above code, I see that the IMG element is added to the external content with width and height of 300px, but the PNG image itself doesn't render. Just see an empty 300px square for the IMG element.
FINAL UPDATE
The answer solved my problem, but with one change. I used File and FileStream classes instead of URLRequest to load the png. Here's what I did:
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);
发布评论
评论(2)
为什么不在运行时以 Base 64 编码 img ?
Why don't encode img in base 64 at runtime ?
placeLoadStringContentInApplicationSandbox
不起作用,因为您的内容不是从字符串加载的,而是从外部网址加载的。并且远程内容被限制使用本地文件。如果您加载了 html 并将其手动设置为htmlText
属性,它将成为本地内容,但这可能会出现问题(例如页面闪烁)。placeLoadStringContentInApplicationSandbox
doesn't work because your content isn't loaded from a string, it's loaded from external url. And remote content is restricted from using local files. If you get loaded html and set it manually tohtmlText
property, it will become local content, but this may have problems (like page flickering.)