Although loading an image in openFL is straight forward, I got no luck loading an image…
My code:
var bitmapData:BitmapData = Assets.getBitmapData("assets/circuits/1/thumb.png");
var bitmap:Bitmap = new Bitmap(bitmapData);
file_view.addChild(bitmap);
However, I got this error:
Assets.hx:149: [openfl.Assets] There is no BitmapData asset with an ID of "assets/circuits/1/thumb.png"
The thumb.png is there, but I still get the error.
I am trying to target windows.
Ok, I found that image will load fine only if I have it stored in assets/circuits/1/ in the project source folder before building the project.
But, if I removed it from project source folder and copied it next to the executable, and try to build or run executable it will not load … ! I don’t know why…
You need to store your assets in the project folder,
they will be copied next to the executable and listed,
you can’t load an asset that wasn’t there during the build since it’s not in the list.
In my project, thumbs are generated and stored in the assets folder, but they can’t be loaded, although images that where there in the project source folder will be loaded fine…
So, I need to store& load new thumbs in production … how?
fromFile only works on native targets, because those targets have access to the file system.
Assets.getBitmapData uses a library cache that embeds resources in such a way - regardless of platform - that it requires mapping to the given directories at compile-time. If you look into ApplicationMain.hx in the haxe output folder, you will see that it will map the resources accordingly.
If the resources are not mapped, the executable won’t find the resource, even if it technically exists.
Thanks tienery for the info, do you mean that Assets.getBitmapData will result in using the same bitmapData for multiple bitmaps? what about loading images using URLRequest? is there a caching for such case in openFL?
I have finally loaded the images by using full path in local storage directory using URLRequest
Yes, if the location is the same as before. When you request a new resource via that very function, it will grab it from its mapped location, relative to the executable folder. If it’s bytecode, like Flash, then it acquires the symbol within its SWF resource.