Alex Smolov
Alex Smolov

Reputation: 1861

Embedding images in ItemRenderer in Flex

If I embed some image in DataGroup as follows:

[Embed(source="/assets/someimage.png")]
private var someimageClass:Class;

Will the image be embedded as many times as there are items in DataGroup? Would it be considered a bad practice?

Upvotes: 0

Views: 255

Answers (1)

fsbmain
fsbmain

Reputation: 5267

Will the image be embedded as many times as there are items in DataGroup

No, image will be embedded only once. You can access it by the name ItemRendererName_someimageClass. Consider your item renderer class name is TT:

    trace(getDefinitionByName("TT_someimageClass"));
    trace(ApplicationDomain.currentDomain.getDefinition("TT_someimageClass"));

output:

[class TT_someimageClass]
[class TT_someimageClass]

Would it be considered a bad practice?

By AS3 code style convention (that used in may opensource projects, as3 livedocs examples and flex) the common way to embed assets is to use public static const for public assets and private static const for private assets, but it seems there isn't any difference in access performance between all these cases, check out this benchmark:

package
{
import flash.display.Sprite;
import flash.utils.getTimer;

public class astest extends Sprite
{
    public function astest()
    {
        var tt1:TT = new TT();

        var clazz:Class;
        var t:uint, i:int;

        t == getTimer();
        for(i = 0; i < 100000; i++)
            clazz = TT.someimageClass2;
        trace("static var:", (getTimer() - t), "ms");

        t == getTimer();
        for(i = 0; i < 100000; i++)
            clazz = TT.someimageClass3;
        trace("static const:", (getTimer() - t), "ms");

        t == getTimer();
        for(i = 0; i < 100000; i++)
            clazz = tt1.someimageClass;
        trace("var:", (getTimer() - t), "ms");

        t == getTimer();
        for(i = 0; i < 100000; i++)
            clazz = tt1.someimageClass1;
        trace("const:", (getTimer() - t), "ms");

        var cache:Class = tt1.someimageClass;
        t == getTimer();
        for(i = 0; i < 100000; i++)
            clazz = cache;
        trace("cache:", (getTimer() - t), "ms");

    }

}
}

package
{

public class TT
{
    [Embed(source="vogel_small.jpg")]
    public var someimageClass:Class;

    [Embed(source="vogel_small.jpg")]
    public const someimageClass1:Class;

    [Embed(source="vogel_small.jpg")]
    public static const someimageClass2:Class;

    [Embed(source="vogel_small.jpg")]
    public static var someimageClass3:Class;

    public function TT()
    {
    }
}
}

output:

static var: 85 ms
static const: 94 ms
var: 102 ms
const: 110 ms
cache: 118 ms

All results are in the range of 10% error, you can swap any two test and get the same time about ~100ms, so there isn't any difference for performance in a way you embed assets.

Upvotes: 1

Related Questions