Search code examples
rustrust-cargo

SVG Example not loading the SVG image in other system in iced library for rust


I tried building the svg example present in the iced library. The system in which it was build, it is working as expected (i.e., the tiger SVG image is loading) but if I'm trying to run it in different machine(laptop), then the SVG image is not loading and only white screen is created.

Please help, how should I proceed in this case:

Working case: machine 1 (where this app was built)

enter image description here

Failure case: machine 2 (where I was testing this app)

enter image description here

My OS is Windows and I'm using the latest version of iced library.

The code I'm using: https://github.com/iced-rs/iced/tree/master/examples/svg

I tried building the svg example present in the iced library. The system in which it was build, it is working as expected (i.e., the tiger SVG image is loading) but if I'm trying to run it in different machine (laptop), then the SVG image is not loading and only white screen is created.


Solution

  • Iced is trying to load your svg at runtime via let handle = svg::Handle::from_path. As this file doesn't exist on the new machine, it's unable to load it. You can instead build the file into the application using rust's include_bytes!(path) macro and replacing from_path with from_memory. The fixed code would look like this:

    let handle = svg::Handle::from_memory(include_bytes!("../resources/tiger.svg").as_slice());
    

    I've tested this code myself before and after moving the .exe to a different location on my computer and it seems to be working.