Incular logoIncular
API ReferenceText & Media

Image Providers & Cache

Asset, file, and memory providers plus the decoded LRU cache.

Cheap Clone descriptions; decoding happens on load.

ProviderSourceLoad
AssetImageApp asset pathImageHandle::from_file
FileImageFilesystem pathImageHandle::from_file
MemoryImageEncoded bytesImageHandle::from_bytes
use incular::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let bytes = vec![200u8; 64];
    let mut cache = ImageCache::new();
    let handle = cache.load_bytes(&bytes).expect("load image");
    let direct = MemoryImage::new(bytes).load().expect("decode image");
    let app = Application::new(move |_cx| {
        Container::builder()
            .padding(EdgeInsets::all(24.0))
            .alignment(Alignment::CENTER)
            .child(
                Column::builder()
                    .main_axis_size(MainAxisSize::Min)
                    .spacing(8.0)
                    .children(Vec::<Widget>::from([
                        Image::new(handle.clone()).width(64.0).height(64.0).into(),
                        Image::new(direct.clone()).width(64.0).height(64.0).into(),
                    ]))
                    .build(),
            )
            .build()
            .into()
    })?;
    incular::run(app)?;
    Ok(())
}

AssetImage::new("assets/logo.png") and FileImage::new("/tmp/photo.png") load through ImageHandle::from_file the same way.

ImageProvider is the Clone-description trait behind all three (load() / load_with(configuration)).

ImageCache

App-owned decoded cache keyed by full bytes. Limits count encoded+decoded bytes; oversize images serve fresh, failures never cache. Defaults: 64 entries, 32 MiB.

use incular::prelude::*;

fn main() {
    let mut cache = ImageCache::new();
    println!("empty: {}", cache.is_empty());
    println!("len: {} bytes: {}", cache.len(), cache.resident_bytes());
    cache.clear();
    println!("cleared empty: {}", cache.is_empty());
}

ImageCacheDiagnostics { loads_requested, load_cache_hits, load_failures, image_decodes, evictions }.

DecodedImage::from_rgba8(width, height, pixels); max decode 16384 px / 256 MiB. ImageError: Io / Decode / InvalidDimensions / InvalidRgbaLength / DecodeTooLarge. ImageId(u64), ImageSource: Embedded / Memory / FileSystem / Generated.

Related: Image.

On this page