How to render a picture through the data of Vec<u8> #13857
-
What problem does this solve or what need does it fill?I want to render the picture to ImageBundle through Vec I'm not sure if I haven't found out how to use it, or I don't have this feature. What solution would you like?I hope UiImage can directly set up Vec What alternative(s) have you considered?No Additional contextI expect it to be used in this way. #[tokio::main]
pub async fn get_img_by_url(url: String) -> Result<Vec<u8>, reqwest::Error> {
let response = reqwest::get(url).await?.bytes().await?;
Ok(response.to_vec())
}
match get_img_by_url("xxx") {
Ok(data) => {
let img = UiImage {
texture: data,
..Default::default()
};
}
Err(e) => {
println!("Get img failed: {:?}", e);
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 1 reply
-
Here is some code I've used to take image data and create an image of it: fn foo(assets: Res<AssetServer>, ..) {
..
let image = Image::new(
Extent3d { width, height, depth_or_array_layers: 1 },
TextureDimension::D2,
data,
bevy::render::render_resource::TextureFormat::Rgba8UnormSrgb,
RenderAssetUsages::default(),
);
let image_handle: Handle<Image> = assets.add(image);
// Can now use this handle in e.g. a standard material
} Since you want Also the image creation needs data in the correct layout. let img = image::open(image_path).unwrap();
let img = img.resize(640, 480, image::imageops::FilterType::CatmullRom);
let data = img.into_rgba8().into_vec(); |
Beta Was this translation helpful? Give feedback.
-
Thank you. This is my final realization. match network::get_img_by_url(src.clone()) {
Ok(data) => match image::load_from_memory(&data) {
Ok(img) => {
let i = Image::from_dynamic(img, true, RenderAssetUsages::default());
img_bundle.image = UiImage {
texture: asset_server.add(i),
..Default::default()
}
}
Err(e) => {
println!("Load img failed: {:?}", e);
}
},
Err(e) => {
println!("Get img failed: {:?}", e);
}
}; |
Beta Was this translation helpful? Give feedback.
Here is some code I've used to take image data and create an image of it:
Since you want
UiImage
and that has a field forHandle<Image>
that should work.Also the image creation needs data in the correct layout.
Here is one way I've gotten the above to work when using the
Image
cr…