简体   繁体   中英

How to Use RustEmbed to load Images into an fltk app?

I'm trying to use the RustEmbed crate to embed some svg icons into my executable. I don't see any documentation on how to turn the data returned from Asset::get("icons/some.svg") into an actual image. My GUI library is fltk , and I want to create an fltk::image::SvgImage using Asset but SvgImage::new() loads an svg from a path, not raw byte data. Because it loads the svg from a path and not from raw byte data, does that mean I can't use RustEmbed for embedding the icons into my target build?

I want to do this because I feel like embedding my image assets into the executable will help avoid IO errors when the path of my executable is changed during deployment/install/build. I thought this one of the intentions of the RustEmbed crate.

use rust_embed::RustEmbed;
use fltk::image::*;
#[derive(RustEmbed)]
#[folder = "examples/public/"]
struct Asset;

fn main() {
  let svg = Asset::get("icons/eye.svg").unwrap();
  println!("{:?}", std::str::from_utf8(svg.as_ref()));
  //just prints some long array of numbers [60, 115,118,...60,47,115]
  wind_svg = SvgImage::load(svg).unwrap();
}

gives error:

the trait `AsRef<std::path::Path>` is not implemented for `std::option::Option<Cow<'_, [u8]>>`

Turns out that fltk::image::SvgImage has a from_data() function. This can be used create load the svg from byte data:

use rust_embed::RustEmbed;
use fltk::image::*;
#[derive(RustEmbed)]
#[folder = "examples/assets/"]
struct Asset;

fn main() {
  let bytes = Asset::get("icons/eye.svg").unwrap();
  let svg = SvgImage::from_data(std::str::from_utf8(&bytes).unwrap()).unwrap();
}

For more useful information on dependencies check out this reddit thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM