I'm new to rust in general. Sorry if this question is overly basic.
I'm trying to display an image using the iced library:
fn view(self: &UI) -> iced::Element<'_, Message> {
iced::widget::image("res/imgs/Monument_valley_by_orbitelambda.jpg").into()
}
This works fine in the happy case. However if the referenced file either doesn't exist, or is not a valid image file, the program will just display a blank window. I would like some way to handle the error cases; but I cannot find any indication of how to do so in the docs.
image expects an image Handle, a file path is the most convenient way to create a Handle so it's used everywhere in the examples, but you can also create a Handle from an in-memory buffer of bytes. when loading the buffer, you can handle errors however you see fit:
// prepare the image data, e.g. load from a file, or download from a URL
// replace the `.expect()` with your own error handling
let bytes = std::fs::read("path/to/image").expect("load image file");
let handle = Handle::from_bytes(bytes);
let widget = image(handle);
let element = widget.into();
note, Handle is cheap to clone, you should load the image data and create the Handle upfront and store it in your app state. when rendering the ui, just clone the handle for the image (or, you can pass a reference as argument to image(), but it just clones the handle implicitly). DON'T load the image file every frame.