Probably a very stupid question but how can you store an image in code? So you do not see it in the Resource folder.
You could convert the image to a BASE64 string and store it in a constant, then, when the app loads or when you need the image, convert back to a Picture object from the BASE64 string.
https://documentation.xojo.com/api/graphics/picture.html#picture-todata
https://documentation.xojo.com/api/graphics/picture.html#picture-fromdata
https://documentation.xojo.com/api/text/encoding_text/encodebase64.html
https://documentation.xojo.com/api/text/encoding_text/decodebase64.html
Thanks.
Edit: it looks like png files aren’t possible.
They are. I do it all the time. PNG is the best supported format in Xojo at the moment, in my opinion.
- If your images are simple enough, you can use code to draw them.
- I personally wouldn’t recommend storing encoded images in your source code as this will add more weight to Xojo, uses more memory (as the encoded version is loaded into RAM, along with the image object).
- You could use Asset Catalogues, but you then must use API to load the image and these are slow.
- You could create your own binary file and store image data in there.
- You could hack the meta data section of the Mach-O executable and store the image data there.
- You might be able to append the binary image data to the end of the Mach-O file (I’ve not experimented with this technique so I don’t know if the Mach-O loader will like it).
- You could use a SQLiteDatabase (but you’d have to copy it to a different location before you could open it).
I agree with Sam. I’ve done this before, it’s not worth the effort. Maintenance is a pain, and in a few years you’ll forget exactly what you did to get the images in there in the first place.
Mind sharing the reason why you want to do this? We might have better ideas.
Using a SQLite database is probably the best option, and I should’ve said that you really only want to use constants if the images are small (32x32 or smaller, and not a whole lot of them), otherwise you impact loading and performance (of the IDE and your app) noticeably.
What if you clear the encoded string/memory block after you converted it into a Picture?
That shouldn’t slow down anything or use more memory compared to the images stored in the Resources.
Or am I wrong?
AFAIK Code is loaded into memory, if your image is stored in code (I believe even constants are loaded into memory), the encoded version is loaded into memory and as far as I know, there is no official way to unload code, maybe if you store this in a PlugIn, Framework or Bundle you can unload it.
Actually… I just thought, you could encode only the first x bytes of an image, basically the meta data of the image, leaving the image data intact, that way no other application could read the image file, but when you need it, you load first x * 2 bytes, decode them, load the rest of the data and then use picture.fromData to decompress into a picture object.