How to load a image/texture then clear it from memory later? #5367
-
I have been using egui::Image::new to load an image from a texture, however, how do I later unload the image from memory? Here is my Code: // Struct to hold the values (context)
pub struct Wiki {
current_images: Arc<Mutex<HashMap<String, ImageData>>>,
loading_images: Arc<Mutex<HashSet<String>>>, // Track which images are still loading
}
// Struct to hold png image data (context)
struct ImageData {
data: Vec<u8>,
width: usize,
height: usize,
}
// To actually render the image
fn render_image(app_self: &Wiki, ctx: &egui::Context, ui: &mut egui::Ui, image_spec: &ImageSpec) {
let (image_name, max_width, max_height) = match image_spec {
ImageSpec::Simple(path) => (path, 800.0, 600.0), // Default sizes
ImageSpec::Detailed { path, max_width, max_height } => (path, max_width.unwrap_or(800.0), max_height.unwrap_or(600.0)),
};
if let Ok(images) = app_self.current_images.lock() {
if let Some(image_data) = images.get(image_name) {
let texture = ctx.load_texture(image_name, egui::ColorImage::from_rgba_unmultiplied([image_data.width, image_data.height], &image_data.data), egui::TextureOptions::default());
let aspect_ratio = image_data.width as f32 / image_data.height as f32;
let display_width = if image_data.width as f32 > max_width { max_width } else { image_data.width as f32 };
let display_height = display_width / aspect_ratio;
let display_height = if display_height > max_height { max_height } else { display_height };
ui.add(egui::Image::new(&texture).max_size(egui::vec2(display_width, display_height)));
} else if let Ok(loading) = app_self.loading_images.lock() {
if loading.contains(image_name) {
ui.spinner();
ui.label("Loading image...");
} else {
ui.colored_label(egui::Color32::RED, "Failed to load image");
}
}
}
} I clear like this // Clear existing data
if let Ok(mut images) = self.current_images.lock() {
images.clear();
}
if let Ok(mut loading) = self.loading_images.lock() {
loading.clear();
} However, these are not where the textures are held. From my understanding Any ideas on how I can do this? Or does dereferenceing/clearing it from current_images clear the texture? Does |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Use
|
Beta Was this translation helpful? Give feedback.
-
https://docs.rs/egui/latest/egui/struct.Context.html#method.forget_all_images and https://docs.rs/egui/latest/egui/struct.Context.html#method.forget_image |
Beta Was this translation helpful? Give feedback.
Use
context.forget_image(uri);
.You can check if it has been cleared in
inspection_ui()
.