首页 > 解决方案 > 在使用 cargo-bundle 构建 .app 后,如何从不断变化的路径环境中加载图像?

问题描述

我正在使用cargo-bundle为使用 fltk-rs 构建的 Rust 应用程序创建一个 .app 包。应用程序中有资产,例如图像。在我开发时,访问这些资产没有问题。

main.rs:

let mut my_img = SharedImage::load("imgs/smiley.png").unwrap();
.
├── Cargo.lock
├── Cargo.toml
├── app_icon_design.psd
├── imgs
│   └── smiley.PNG <--get this image
├── src
│   ├── app_icon.png
│   ├── app_icon@2x.png
│   ├── icon32x32.png
│   └── main.rs
└── target
    ├── CACHEDIR.TAG
    ├── debug
    └── release

然后我捆绑到一个 .app,cargo build --release它在我的 .app 中提供了以下目录结构:

.
├── Info.plist
├── MacOS
│   └── build_to_app_bundle <--my executable
└── Resources
    └── imgs
        └── smiley.PNG<--get this image now

现在我的应用程序需要从/Resources文件夹中获取图像文件:

println!("my resources folder is: {:?}", std::env::current_exe().unwrap().parent().unwrap().join(Path::new("Resources")));
let my_resources_path = std::env::current_exe().unwrap().parent().unwrap().join(Path::new("Resources"));

我怎样才能做到每次我想从路径加载图像时都不需要显式引用该Resources值?

标签: rust

解决方案


推荐阅读