在运行时将JPG转换为Class

时间:2021-12-13 21:02:28

Some methods in the Flex API such as CursorManager.setCursor(cursorClass:Class, priority:int = 2, xOffset:Number = 0, yOffset:Number = 0) take a parameter of type Class for a graphic. This example lets you specify a JPG/BMP/SWF as the cursor, but the only way I know to get a Class from an image file is using [Embed] in MXML, and this needs to be done dynamically in AS3 in my case.

Flex API中的某些方法(如CursorManager.setCursor(cursorClass:Class,priority:int = 2,xOffset:Number = 0,yOffset:Number = 0)为图形采用Class类型的参数。此示例允许您指定JPG / BMP / SWF作为游标,但我知道从图像文件中获取类的唯一方法是在MXML中使用[Embed],这需要在我的情况下在AS3中动态完成。

There must be a standard solution? Is there a good reason these Flex classes take a Class in the first place?

必须有标准的解决方案吗?这些Flex类首先采用Class是否有充分的理由?

EDIT: I don't actually care about emulating the [Embed] tag's behaviour. I just want to be able to use an image file path with CursorManager. I'm sure I've seen this done in other situations, and surely the Flex people figured these things would not always be hard-coded?

编辑:我实际上并不关心模仿[Embed]标签的行为。我只是想能够使用CursorManager的图像文件路径。我确信我已经在其他情况下看到了这一点,而且Flex人们肯定认为这些东西并不总是硬编码的?

EDIT2: To further simplify the problem, what I'm ideally looking for is a way to do the following:CursorManager.setCursor(someHandyFunction("myCursor.png")) If this is even possible, the question is what someHandyFunction should do?!

编辑2:为了进一步简化问题,我正在寻找的是一种方法来执行以下操作:CursorManager.setCursor(someHandyFunction(“myCursor.png”))如果这是可能的,问题是someHandyFunction应该做什么? !

4 个解决方案

#1


This one looked interesting, so I decided to take a whack at it. Here's what I was able to do -- there's probably a more elegant solution, but I've tested this, and it definitely works (here's a working example), so I figured I'd kick it back over to you to tinker with further if you like.

这个看起来很有趣,所以我决定对它进行打击。这就是我能够做到的 - 这可能是一个更优雅的解决方案,但我已经对此进行了测试,它确实有效(这是一个有效的例子),所以我想我会再给你一个补充。如果你喜欢。

First, you'll need a custom class extending DisplayObject -- I just chose Bitmap, since I knew you were attempting to load and use JPG imagery:

首先,你需要一个扩展DisplayObject的自定义类 - 我只选择了Bitmap,因为我知道你正在尝试加载和使用JPG图像:

package
{
    import flash.display.Bitmap;
    import mx.core.Application;

    public class MyLoadedImageClass extends Bitmap
    {       
        public function MyLoadedImageClass()
        {
            // ClassRef is simply the name of my Flex app
            super(ClassRef(Application.application).bitmapData);
        }
    }
}

... and then here's the Application code, which just loads things up and then calls setCursor():

...然后这是应用程序代码,它只是加载东西然后调用setCursor():

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">

    <mx:Script>
        <![CDATA[

            import mx.managers.CursorManager;
            import mx.managers.CursorManagerPriority;

            // This public member holds a reference to your loaded bitmapData,
            // which MyLoadedImageClass's constructor will use when instantiated
            // by the framework during CursorManager.setCursor()
            public var bitmapData:BitmapData;

            // Here we load the image
            private function init():void
            {
                var urlLoader:URLLoader = new URLLoader();
                var urlRequest:URLRequest = new URLRequest("http://roaming.turbonerd.com/m/20090104094515.jpg");

                urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
                urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
                urlLoader.load(urlRequest); 
            }

            // Here we translate the bytes into a Bitmap
            private function urlLoader_complete(event:Event):void
            {
                var bytes:ByteArray = URLLoader(event.target).data;

                var loader:Loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
                loader.loadBytes(bytes); 
            }

            // And finally, we save off the bytes and set the cursor 
            private function loader_complete(event:Event):void
            {
                bitmapData = Bitmap(event.target.content).bitmapData;
                CursorManager.setCursor(MyLoadedImageClass, CursorManagerPriority.HIGH);
            }

        ]]>
    </mx:Script>

</mx:Application>

Hopefully the commenting describes things in enough detail. Hope it helps! (And thanks for the challenge!)

希望评论能够详细描述事物。希望能帮助到你! (并感谢您的挑战!)

#2


Check out this example class from Ben Stucki. I have used it extensively to dynamically create class instances from remote JPG/GIF/PNG.

看看Ben Stucki的这个示例课程。我已广泛使用它从远程JPG / GIF / PNG动态创建类实例。

http://blog.benstucki.net/?p=42

#3


I've recently fought with the CursorManager (tried to make him load png cursors at run-time), and as a result, i've posted a utility class at my labs blog.

我最近和CursorManager一起战斗(试图让他在运行时加载png游标),结果,我在我的实验室博客上发布了一个实用程序类。

#4


As far as I know it is currently not possible to create classes at runtime inside a flash vm using as3. Theoretically you might be able to create a swf with the class you need at runtime by writing swf-data to a bytearray and loading that, I do not recommend you to try that unless you are into a lot of puzzling and don't mind a lot of frustration ;-)

据我所知,目前无法使用as3在flash vm内的运行时创建类。从理论上讲,你可以通过将swf-data写入bytearray并加载它来创建一个运行时所需类的swf,我建议你不要试试这个,除非你遇到很多令人费解的事情并且不介意很多挫折;-)

If you elaborate a bit on your requirements it might be possible to find another solution.

如果您详细说明您的要求,可能会找到另一种解决方案。

BTW: if you don't mind a solution that contains a server component: You could make your server compile new swf's that contain the right class and load those. There are multiple ways of making the flex compiler compile stuff on request.

顺便说一句:如果你不介意包含服务器组件的解决方案:你可以让你的服务器编译包含正确类的新swf并加载它们。有多种方法可以让flex编译器根据请求编译。

#1


This one looked interesting, so I decided to take a whack at it. Here's what I was able to do -- there's probably a more elegant solution, but I've tested this, and it definitely works (here's a working example), so I figured I'd kick it back over to you to tinker with further if you like.

这个看起来很有趣,所以我决定对它进行打击。这就是我能够做到的 - 这可能是一个更优雅的解决方案,但我已经对此进行了测试,它确实有效(这是一个有效的例子),所以我想我会再给你一个补充。如果你喜欢。

First, you'll need a custom class extending DisplayObject -- I just chose Bitmap, since I knew you were attempting to load and use JPG imagery:

首先,你需要一个扩展DisplayObject的自定义类 - 我只选择了Bitmap,因为我知道你正在尝试加载和使用JPG图像:

package
{
    import flash.display.Bitmap;
    import mx.core.Application;

    public class MyLoadedImageClass extends Bitmap
    {       
        public function MyLoadedImageClass()
        {
            // ClassRef is simply the name of my Flex app
            super(ClassRef(Application.application).bitmapData);
        }
    }
}

... and then here's the Application code, which just loads things up and then calls setCursor():

...然后这是应用程序代码,它只是加载东西然后调用setCursor():

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="init()">

    <mx:Script>
        <![CDATA[

            import mx.managers.CursorManager;
            import mx.managers.CursorManagerPriority;

            // This public member holds a reference to your loaded bitmapData,
            // which MyLoadedImageClass's constructor will use when instantiated
            // by the framework during CursorManager.setCursor()
            public var bitmapData:BitmapData;

            // Here we load the image
            private function init():void
            {
                var urlLoader:URLLoader = new URLLoader();
                var urlRequest:URLRequest = new URLRequest("http://roaming.turbonerd.com/m/20090104094515.jpg");

                urlLoader.addEventListener(Event.COMPLETE, urlLoader_complete);
                urlLoader.dataFormat = URLLoaderDataFormat.BINARY;
                urlLoader.load(urlRequest); 
            }

            // Here we translate the bytes into a Bitmap
            private function urlLoader_complete(event:Event):void
            {
                var bytes:ByteArray = URLLoader(event.target).data;

                var loader:Loader = new Loader();
                loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
                loader.loadBytes(bytes); 
            }

            // And finally, we save off the bytes and set the cursor 
            private function loader_complete(event:Event):void
            {
                bitmapData = Bitmap(event.target.content).bitmapData;
                CursorManager.setCursor(MyLoadedImageClass, CursorManagerPriority.HIGH);
            }

        ]]>
    </mx:Script>

</mx:Application>

Hopefully the commenting describes things in enough detail. Hope it helps! (And thanks for the challenge!)

希望评论能够详细描述事物。希望能帮助到你! (并感谢您的挑战!)

#2


Check out this example class from Ben Stucki. I have used it extensively to dynamically create class instances from remote JPG/GIF/PNG.

看看Ben Stucki的这个示例课程。我已广泛使用它从远程JPG / GIF / PNG动态创建类实例。

http://blog.benstucki.net/?p=42

#3


I've recently fought with the CursorManager (tried to make him load png cursors at run-time), and as a result, i've posted a utility class at my labs blog.

我最近和CursorManager一起战斗(试图让他在运行时加载png游标),结果,我在我的实验室博客上发布了一个实用程序类。

#4


As far as I know it is currently not possible to create classes at runtime inside a flash vm using as3. Theoretically you might be able to create a swf with the class you need at runtime by writing swf-data to a bytearray and loading that, I do not recommend you to try that unless you are into a lot of puzzling and don't mind a lot of frustration ;-)

据我所知,目前无法使用as3在flash vm内的运行时创建类。从理论上讲,你可以通过将swf-data写入bytearray并加载它来创建一个运行时所需类的swf,我建议你不要试试这个,除非你遇到很多令人费解的事情并且不介意很多挫折;-)

If you elaborate a bit on your requirements it might be possible to find another solution.

如果您详细说明您的要求,可能会找到另一种解决方案。

BTW: if you don't mind a solution that contains a server component: You could make your server compile new swf's that contain the right class and load those. There are multiple ways of making the flex compiler compile stuff on request.

顺便说一句:如果你不介意包含服务器组件的解决方案:你可以让你的服务器编译包含正确类的新swf并加载它们。有多种方法可以让flex编译器根据请求编译。