You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何无法调用egui_extras::RetainedImage::from_image_bytes方法?

问题:egui_extras::RetainedImage 找不到 from_image_bytes 方法?

我参考egui官方仓库的retained_image示例代码,尝试使用egui_extras::RetainedImage将图片加载到框架中,但报错提示RetainedImage中不存在from_image_bytes方法。我已检查image.rs文件确认该方法存在,以下是我的代码:

use eframe::{run_native, epi::App, egui, NativeOptions};
use egui_extras::RetainedImage;
struct InitView {
    image: RetainedImage,
    tint: egui::Color32,
}

impl Default for InitView {
    fn default() -> Self {
        Self {
            image: RetainedImage::from_image_bytes(
                "date_backdrop.png",
                include_bytes!("date_backdrop.png"),
            )
            .unwrap(),
            tint: egui::Color32::from_rgb(255, 0, 255),
        }
    }
}


impl App for InitView {
    fn name(&self) -> &str {
        "CheckIt"
    }

    fn update(&mut self,ctx: &eframe::egui::CtxRef,frame: &mut eframe::epi::Frame<'_>) {
        //background color
        let frame = egui::containers::Frame {
            fill: egui::Color32::from_rgb(241, 233, 218),
            ..Default::default()
        };
    
        //main window
        egui::CentralPanel::default().frame(frame).show(ctx, |ui| {
            ui.label("test");
        });
    }
}

fn main(){
    let app: InitView = InitView{..Default::default()};
    let win_options = eframe::NativeOptions{
        initial_window_size: Some(egui::Vec2::new(386.0, 636.0)),
        always_on_top: true,
        resizable: false,
        ..Default::default()
    };
    run_native(Box::new(app), win_options);
}

问题原因及解决办法

  • 版本不匹配:你本地依赖的egui、eframe、egui_extras版本和示例代码的版本不一致。from_image_bytes方法可能是在较新版本中新增的,或是旧版本中该方法名称不同/未实现。
  • 同步依赖版本:打开Cargo.toml,确保egui相关依赖的版本和示例使用的版本一致。如果示例用的是git仓库的最新代码,你也可以直接依赖git版本:
    egui = { git = "https://github.com/emilk/egui.git" }
    eframe = { git = "https://github.com/emilk/egui.git" }
    egui_extras = { git = "https://github.com/emilk/egui.git" }
    
  • 替代方法:如果暂时无法升级版本,可尝试使用RetainedImage::from_bytes方法,它的功能和from_image_bytes一致,用法相同:
    RetainedImage::from_bytes("date_backdrop.png", include_bytes!("date_backdrop.png")).unwrap()
    
  • 检查feature配置:部分方法需要启用特定feature才能使用,确认Cargo.toml中egui_extras的feature包含image:
    egui_extras = { version = "x.x.x", features = ["image"] }
    

内容的提问来源于stack exchange,提问作者Terrell-byte

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 10:10:32