跳到正文

Image

使用保留式图像资源显示文件系统路径、文件 URL 或 base64 数据 URL。

正在加载交互演示…

#导入

从 quickgui 导入 Image。

use quickgui::*;

#用法

img(Image::from_rgba(128, 128, pixels).expect("image"))
    .size(160.0, 160.0)
    .rounded(12.0)

#API 参考

展开条目查看说明和完整签名。短横线表示声明中未记录默认值。

pub fn from_rgba( width: u32, height: u32, rgba: impl Into<Arc<[u8]>>, ) -> Result<Self, ImageError>

Construct an image from tightly packed, straight-alpha RGBA8 pixels.

decodepub fn decode(encoded: impl AsRef<[u8]>) -> Result<Self, ImageError>—

Decode PNG, JPEG, TIFF, WebP, or the first frame of a GIF from memory. Decoding is explicit and synchronous. Applications should perform it away from latency- sensitive input handling and then publish the resulting cheap-to-clone `Image`.

pub fn decode(encoded: impl AsRef<[u8]>) -> Result<Self, ImageError>
查看源码
openpub fn open(path: impl AsRef<Path>) -> Result<Self, ImageError>—

Decode a supported image file synchronously with encoded and decoded size limits. Prefer passing a path to [`crate::img`] for event-driven background loading. This method is useful when an application already owns a background execution context. Paths whose stem ends in `Template`, optionally followed by a scale suffix such as `@2x`, are marked for macOS template rendering. Call [`Self::template`] to override that convention.

pub fn open(path: impl AsRef<Path>) -> Result<Self, ImageError>
查看源码
widthpub fn width(&self) -> u32—

Configures width for this part.

pub fn width(&self) -> u32
查看源码
heightpub fn height(&self) -> u32—

Configures height for this part.

pub fn height(&self) -> u32
查看源码
sizepub fn size(&self) -> Size—

Configures size for this part.

pub fn size(&self) -> Size
查看源码
byte_lenpub fn byte_len(&self) -> usize—

Configures byte len for this part.

pub fn byte_len(&self) -> usize
查看源码
rgbapub fn rgba(&self) -> &[u8]—

Tightly packed, straight-alpha RGBA8 pixels.

pub fn rgba(&self) -> &[u8]
查看源码
from_data_urlpub fn from_data_url(url: &str) -> Result<Self, ImageError>—

Decode a base64 `data:` URL carrying PNG, JPEG, GIF, or WebP bytes. Only base64 payloads are accepted; percent-encoded `data:` URLs are rejected because the binary formats QuickGUI decodes are never sent that way in practice. The URL text is bounded by [`MAX_IMAGE_DATA_URL_BYTES`] and the decoded payload by the same limits as [`Image::decode`].

pub fn from_data_url(url: &str) -> Result<Self, ImageError>
查看源码
named_systempub fn named_system(name: &str) -> Result<Self, ImageError>—

Rasterize a system-provided image at [`DEFAULT_SYSTEM_IMAGE_POINT_SIZE`] and [`DEFAULT_SYSTEM_IMAGE_SCALE`]. On macOS this resolves an `NSImage` name first and then an SF Symbol name. Other platforms report [`ImageError::SystemImageUnavailable`].

pub fn named_system(name: &str) -> Result<Self, ImageError>
查看源码
named_system_sizedpub fn named_system_sized(name: &str, point_size: f32, scale: f32) -> Result<Self, ImageError>—

Rasterize a system-provided image at a requested point size and backing scale. The resulting bitmap is `round(point_size * scale)` pixels on its longest axis.

pub fn named_system_sized(name: &str, point_size: f32, scale: f32) -> Result<Self, ImageError>
查看源码
templatepub fn template(&self, template: bool) -> Self—

Return a copy marked as a macOS template image. A template image is recolored by AppKit for the menu bar's light, dark, and highlighted appearances using only its alpha channel. The returned value has a fresh identity so a renderer cache never confuses it with the untemplated original; the pixels are shared.

pub fn template(&self, template: bool) -> Self
查看源码
is_templatepub fn is_template(&self) -> bool—

Whether this image is marked for macOS template rendering.

pub fn is_template(&self) -> bool
查看源码
with_representationspub fn with_representations( &self, representations: impl IntoIterator<Item = (f32, Self)>, ) -> Result<Self, ImageError>—

Attach additional backing-scale representations used when building a native image. The receiver is the 1x representation. At most [`MAX_IMAGE_REPRESENTATIONS`] variants are retained and each scale must lie between [`MIN_IMAGE_REPRESENTATION_SCALE`] and [`MAX_IMAGE_REPRESENTATION_SCALE`].

pub fn with_representations( &self, representations: impl IntoIterator<Item = (f32, Self)>, ) -> Result<Self, ImageError>
查看源码
representationspub fn representations(&self) -> &[(f32, Self)]—

Additional backing-scale representations attached to this image.

pub fn representations(&self) -> &[(f32, Self)]
查看源码
resizepub fn resize(&self, width: u32, height: u32) -> Result<Self, ImageError>—

Resample this image to exact pixel dimensions with a bilinear filter.

pub fn resize(&self, width: u32, height: u32) -> Result<Self, ImageError>
查看源码
croppub fn crop(&self, bounds: Rect) -> Result<Self, ImageError>—

Copy a pixel rectangle out of this image. The rectangle is rounded to whole pixels and must lie inside the image with a non-empty area.

pub fn crop(&self, bounds: Rect) -> Result<Self, ImageError>
查看源码
to_pngpub fn to_png(&self) -> Result<Vec<u8>, ImageError>—

Encode this image as PNG with its alpha channel intact.

pub fn to_png(&self) -> Result<Vec<u8>, ImageError>
查看源码
to_jpegpub fn to_jpeg(&self, quality: u8) -> Result<Vec<u8>, ImageError>—

Encode this image as JPEG at `quality` (1-100), compositing away the alpha channel. JPEG has no alpha, so translucent pixels are composited over opaque white.

pub fn to_jpeg(&self, quality: u8) -> Result<Vec<u8>, ImageError>
查看源码
pub fn img(source: impl Into<ImageSource>) -> Element

Create an image element with intrinsic sizing and `object-fit: contain` behavior.