Function image

Source
pub fn image(args: impl Into<ImageArgs>)
Expand description

A component that renders an image.

The image component displays an image based on the provided ImageData. It can be explicitly sized or automatically adjust to the intrinsic dimensions of the image. For optimal performance, image data should be loaded and decoded before being passed to this component, for example, by using the load_image_from_source function.

§Arguments

  • args - The arguments for the image component, which can be an instance of ImageArgs or anything that converts into it (e.g., ImageData).

§Example

use std::sync::Arc;
use tessera_ui_basic_components::{
    image::{image, load_image_from_source, ImageArgsBuilder, ImageSource, ImageData},
};
use tessera_ui::{Dp, DimensionValue};

// In a real application, you would load the image data once and store it.
// The `include_bytes!` macro is used here to load file contents at compile time.
// For dynamic loading from a file path, you could use `ImageSource::Path`.
let image_bytes = Arc::new(*include_bytes!("../../example/examples/assets/scarlet_ut.jpg"));
let image_data = load_image_from_source(&ImageSource::Bytes(image_bytes))
    .expect("Failed to load image");

// Renders the image with its intrinsic size by passing `ImageData` directly.
image(image_data.clone());

// Renders the image with a fixed width using `ImageArgs`.
image(
    ImageArgsBuilder::default()
        .data(image_data)
        .width(DimensionValue::Fixed(Dp(100.0).into()))
        .build()
        .unwrap(),
);