代码之家  ›  专栏  ›  技术社区  ›  alexey

无法访问HTML文本中的嵌入图像

  •  3
  • alexey  · 技术社区  · 16 年前

    图像可以包括在 TextArea 控件使用 htmlText 财产:

    ta.htmlText = '<img src="http://..."/>';
    

    我怎么能 参考嵌入图像 ?

    一个例子:

    <mx:TextArea xmlns:mx="http://www.adobe.com/2006/mxml">
        <mx:Script>
            <![CDATA[
                [Embed(source='../assets/img.gif')]
                public var img:Class;
            ]]>
        </mx:Script>
        <mx:htmlText>
            <![CDATA[
                <img src="???" />
            ]]>
        </mx:htmlText>
    </mx:TextArea>
    

    UPD:

    <img src='../assets/img.gif />
    

    在本地计算机上工作,但在服务器环境中会引发:

    Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.
    

    我怎么修这个?

    9 回复  |  直到 12 年前
        1
  •  2
  •   dirkgently    16 年前

    尝试:

    <mx:htmlText>
            <![CDATA[
               <p>
                <img src='../assets/butterfly.gif' 
                     width='30' height='30' 
                     align='left' 
                     hspace='10' vspace='10'>
                </p>
            ]]>
         </mx:htmlText>
    

    documentation .

        2
  •  10
  •   Sly_cardinal    15 年前

    好吧,我刚刚花了几个小时把它整理出来,但我已经把它用在flash和flex上了。

    在文本字段中显示图像

    可以将DisplayObjects加载到textfield中 <img /> 标签包括movieclips、sprites和嵌入图像。

    • 在flash或flex中,如果要显示嵌入的图像,则必须创建一个扩展 DisplayObject 类(例如sprite或movieclip)。

    • 确保文本字段足够大,可以包含图像。在测试过程中,当我的文本字段太小而无法显示整个图像时,我认为事情没有起作用。


    闪速示例

    简单的例子,所有代码都在主时间线上。

    1. 在阶段上创建动态文本字段。给它一个有用的名字,我叫我的 txtImageTest .

    2. 调整大小 最新节目 尺寸合适,如300x150px。

    3. 创建一个新的movieclip符号并给它一个类名,例如 imageClip1 .

    4. 在新剪辑中绘制内容或在其中放置嵌入图像 图像1 .

    5. 返回主时间线,取消选择所有对象并在第一帧上打开ActionScript编辑器。

    6. 在文本字段上启用多行和换行:

      imageClip1.wordWrap = true;
      imageClip1.multiline = true;
      imageClip1.htmlText = "<p>You can include an image in your HTML text with the &lt;img&gt; tag.</p><p><img id='testImage' src='imageClip1' align='left' width='30' height='30' hspace='10' vspace='10'/>Here is text that follows the image. I'm extending the text by lengthening this sentence until it's long enough to show wrapping around the bottom of the image.</p>"
      
    7. 保存并测试您的电影。


    柔性实例

    因为我们不能像在flash中那样在库中创建一个新的movieclip,所以我们需要创建一个执行相同任务的新类(如果您想在库中创建一个新剪辑,这个方法也可以在flash中工作)。

    这是我的一个黑色三角形子弹班,叫做黑箭头。

    // Set the correct package for you class here.
    package embed
    {
        import flash.display.Sprite;
        import mx.core.BitmapAsset;
    
        public class BlackArrow extends Sprite
        {
            // Embed the image you want to display here.
            [Embed(source='assets/embed/triangleIcon_black.png')]
            [Bindable]
            private var TriangleImage:Class;
    
            public function BlackArrow()
            {
                super();
    
                // Instantiate the embedded image and add it to your display list.
                var image:BitmapAsset = new TriangleImage();
                addChild(image);
            }
    
        }
    }
    

    注: 扩展位图(在闪存中)或位图资产(在flex中),因为它们不能在文本字段中正确缩放或定位。选择雪碧或类似的东西。

    下面是在文本字段中显示此图像的类的示例:

    <?xml version="1.0" encoding="utf-8"?>
    <mx:VBox xmlns:mx="http://www.adobe.com/2006/mxml"
        width="100%" height="100%">
    
        <mx:Script>
        <![CDATA[
            import embed.BlackArrow;
    
            // You must include a variable declaration of the same type as your
            // wrapper class, otherwise the class won't be compiled into
            // the SWF and you will get an IOError.
            private var img2:BlackArrow;
        ]]>
        </mx:Script>
    
        <mx:Text id="txtResults1" width="100%" height="100%">
            <mx:htmlText>
            <![CDATA[<p>You can include an image in your HTML text with the &lt;img&gt; tag.</p><p><img id='testImage' src='embed.BlackArrow' align='left' hspace='10' vspace='10'/>Here is text that follows the image. I'm extending the text by lengthening this sentence until it's long enough to show wrapping around the bottom of the image.</p>]]>
            </mx:htmlText>
        </mx:Text>
    
     </mx:VBox>
    

    请注意,我在 src 图像标记的属性。


    期末笔记

        3
  •  3
  •   Ryan Wilson    15 年前

    通过使用HTML文本中HTML IMG标记的完全限定类名引用它们,可以将嵌入图像指定为HTML IMG标记的SRC,您可以使用 getFullyQualifiedClassName )像这样:

    public class Example
    {
        [Embed(source="myImage.png")
        public static const MyImage:Class;
    
        public function getHTMLImg() : String
        {
            return "<img src='" + getQualifiedClassName(MyImage) + "' />";
        }
    }
    

    这比为每个可能嵌入的图像创建一个包装类要简单得多…

        4
  •  1
  •   bug-a-lot    15 年前

    我也在找同样的东西。使用背后的想法 嵌入的 图像是为了防止从外部URL加载所述图像。所以公认的答案实际上并不能解决问题。我已经看到了文档,它们也在那里使用了一个URL,并且该图像不会编译到SWF中,而是在运行时从服务器加载。他们之所以称之为嵌入式图像,可能是因为它嵌入了文本,但我要找的是使用嵌入在编译代码中的图像。

    为什么要在HTML文本中使用嵌入式图像显示?因为这是在工具提示中显示图像的最简单方法(您只需要重写为默认工具提示类来设置HTMLTEXT而不是TEXT,并将其设置为工具提示管理器中的默认工具提示类)。在我的搜索中,我发现了: http://groups.google.com/group/flex_india/browse_thread/thread/cf0aa62afaae3fdc/b21f462f8d5da117?pli=1 . 还没有测试它,我想在确定链接ID时会遇到问题。当我有链接ID时,我会提供更多详细信息。

        5
  •  1
  •   user2499859    12 年前

    斯吕·卡迪纳写了一个非常有用的答案。非常感谢他! 我有半天没法解决这个问题。Sly_Cardinal注意到,如果这样做,那么getQualifiedClassName(嵌入位图)图像就不能正确地插入文本的左上角。 狡猾的红衣主教提议做一个包装类。

    我想附加一个他的答案

    一个新的类包装器 图片包装.as->

    package 
    {
    import flash.display.Bitmap;
    import flash.display.Sprite;
    public class ImageWrapper extends Sprite{
        public static var img:Class;
        public function ImageWrapper() {
            var bitmap:Bitmap = new img();
            addChild(bitmap);
        }
    }
    }
    

    代码中的调用:

    [Embed(source = "img/MyImg.png")] var _MyImg:Class;
    ImageWrapper.img = _MyImg;
    tf.htmlText = "My text, and my image <img src='ImageWrapper'>";
    

    就是这样。 谢谢您!

        6
  •  0
  •   CookieOfFortune    16 年前

    尝试

    src='../assets/img.gif'
    

    取自: Using the htmlText property

        7
  •  0
  •   klakomatic    15 年前

    使用src='assets/img.gif',但您必须在部署应用程序swf的服务器目录中创建一个assets目录,并将img.gif文件放在该目录中。在HTML中,它将查找与SWF文件相关的文件。

    这将允许您在开发和部署位置中使用相同的文件和位置。

        8
  •  0
  •   Georgi Popov    15 年前

    它在本地主机上工作,因为您在计算机上有图像,所以可以加载它。如果要嵌入它,它不会上载到指定目录下的服务器上,而是由.swf文件本身包含。在生产输出的相应位置上载图像(从源文件文件夹浏览以进行上载)。

        9
  •  0
  •   Ali    13 年前

    没有必要再打扰我们自己了! 从现在开始进口movieclips就没什么大不了的了!通过名为textfarea的新类(而不是组件的textfarea)的帮助,它是oop类,是原始textfield类的扩展。

    我们不再需要嵌入或库 我们可以在每个地方使用文本区域而不是文本字段,这样我们就可以导入我们自己的SWF文件,比如视频播放器、谈话虚拟人物或文本行中的其他任何内容。

    它有更多的特性,并且不局限于这个问题。 退房 www.doitflash.com 更多信息, 该网站提供平台并免费下载 :)

    推荐文章