我正在尝试使用svg将一些RustEmbed图标嵌入到我的可执行文件中。我没有看到任何关于如何将从Asset::get("icons/some.svg")
返回的数据转换为实际图像的文档。我的GUI库是fltk,我想使用Asset
创建一个fltk::image::SvgImage
,但是SvgImage::new()
从路径加载svg,而不是从原始字节数据加载。因为它从路径而不是从原始字节数据加载svg,这是否意味着我不能使用RustEmbed将图标嵌入到我的目标构建中?
我之所以这样做,是因为我觉得将映像资产嵌入到可执行文件中将有助于避免在部署/安装/构建过程中更改可执行文件的路径时出现IO错误。我认为这是RustEmbed板条箱的意图之一。
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();
}
给出错误:
the trait `AsRef<std::path::Path>` is not implemented for `std::option::Option<Cow<'_, [u8]>>`
发布于 2021-03-06 18:23:54
原来fltk::image::SvgImage有一个from_data()
函数。这可以使用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();
}
有关依赖关系的更多有用信息,请查看this reddit thread。
https://stackoverflow.com/questions/66502590
复制相似问题