Skip to content

Image 图片 ​

展示图片并提供尺寸、裁剪、占位、失败回退和预览能力。

何时使用 ​

  • 头像、封面、缩略图和内容图片展示。
  • 需要点击放大、旋转或浏览图片组时使用预览。
  • 仅需图标时使用 icon 包,避免加载图片。

在线体验 ​

Image 图片 · 在线体验真实 Go 组件
在独立页面打开 ↗

查看此场景的 Go 源码 ↗

基本用法 ​

go
import (
    "image"
    "github.com/tangoui/tango/ui"
)

func page(c *ui.Context) {
    ui.Image(c, image.NewRGBA(image.Rect(0, 0, 320, 180))).Size(320, 180).Alt("示例图片")
}

常见用法 ​

裁剪与圆角 ​

go
func crop(c *ui.Context, img image.Image) {
    ui.Image(c, img).Size(240, 160).Fit("cover").Radius(8)
}

加载和失败回退 ​

go
func loading(c *ui.Context, img, fallback image.Image, pending, failed bool) {
    ui.Image(c, img).Loading(pending).Failed(failed).Fallback(fallback).Alt("产品封面")
}

预览组 ​

go
func gallery(c *ui.Context, imgs []image.Image) {
    if len(imgs) == 0 {
        return
    }
    ui.Image(c, imgs[0]).Size(160, 100).PreviewGroup(imgs, 0).Alt("相册封面")
}

PreviewGroup 启用点击预览并提供上一张 / 下一张操作。PreviewSrc(img) 为缩略图指定单独的预览原图,OpenPreview() 可在按钮点击等事件中打开预览。MinScale、MaxScale 和 ScaleStep 调整缩放,Movable、MaskClosable、CloseOnEscape 控制交互,Infinite 和 ShowProgress 控制图片组循环及页码。

主题与样式 ​

图片令牌负责占位背景、边框、预览遮罩和圆角。Fit 使用 contain 或 cover 等裁剪策略;尺寸优先用 Size 明确给出,避免布局跳动。

注意事项 ​

给内容图设置 Alt 或 A11yLabel;装饰图可使用空替代文本。预览组的索引必须对应图片数组。大图应在进入页面前缩放,避免在即时模式中反复解码。

示例代码 ​

以下是上方在线演示运行的 Go 源码(来自官方 gallery),点击标题展开。函数接收的 c *ui.Context 由窗口的 UI 循环每帧传入,接入方式见 Go GUI 入门。

Image 图片

gallery 演示 Image 的具名 Render 函数,源码 page_data_image.go#L61。

加载中…

Image 图片预览

gallery 演示 ImagePreview 的 Render 函数,源码 page_data_image.go#L99。

加载中…

Image 预览:缩放范围、到头即停与计数

gallery 演示 ImageAlign 的 Render 函数,源码 page_b2h.go#L121。

加载中…

API ​

签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。

入口函数 ​

ui.Image ​

go
func Image(c *Context, img image.Image) *ImageW

Image adds an image; img may be nil (shows the placeholder). The default size is the image's pixel size (200×200 when unknown); use Width/Height.

返回 *ImageW,可链式调用的方法见 ImageW 方法。

ImageW 方法 ​

ui.ImageW:ImageW displays a bitmap with loading placeholder, error fallback and an optional full-screen preview (Ant Design Image / Element Plus el-image).

方法说明签名
A11yLabelA11yLabel sets the accessible name (like Alt, without changing the Driver.Find label).A11yLabel(s string) *ImageW
AltAlt sets the accessible label (also used by Driver.Find).Alt(s string) *ImageW
CloseOnEscapeCloseOnEscape(false) keeps the preview open on Escape (Element close-on-press-escape, default true).CloseOnEscape(on bool) *ImageW
FailedFailed shows the error fallback (broken picture / "加载失败").Failed(on bool) *ImageW
FallbackFallback sets the image shown when loading failed (Ant `fallback`).Fallback(img image.Image) *ImageW
FitFit sets object-fit: "fill" (default), "contain", "cover", "scale-down".Fit(f string) *ImageW
HeightHeight(px float32) *ImageW
IDID returns the widget ID.ID() ID
InfiniteInfinite sets whether the group preview wraps from the last image to the first (Element infinite, default true; Ant stops at the ends).Infinite(on bool) *ImageW
LayoutLayout applies layout options.Layout(o ...layout.Option) *ImageW
LoadingLoading shows the placeholder instead of the image.Loading(on bool) *ImageW
MaskClosableMaskClosable sets whether a click on the preview backdrop closes it (Ant mask.closable, default true; Element hide-on-click-modal, default false; the theme picks the default).MaskClosable(on bool) *ImageW
MaxScaleMaxScale(k float32) *ImageW
MinScaleMinScale and MaxScale bound the preview zoom (Ant minScale 1 / maxScale 50; Element min-scale 0.2 / max-scale 7; the theme picks the default).MinScale(k float32) *ImageW
MovableMovable(false) stops dragging the preview image around (Ant preview.movable, default true).Movable(on bool) *ImageW
OpenPreviewOpenPreview opens the preview now (Ant preview.open); call it once per action, like Rotate.OpenPreview() *ImageW
PreviewPreview enables click-to-preview.Preview(on bool) *ImageW
PreviewAnglePreviewAngle returns the preview rotation in degrees (clockwise).PreviewAngle() float64
PreviewGroupPreviewGroup previews imgs (this image is imgs[index]) with prev/next navigation and a counter.PreviewGroup(imgs []image.Image, index int) *ImageW
PreviewOpenPreviewOpen reports whether the preview overlay is open.PreviewOpen() bool
PreviewScalePreviewScale returns the preview zoom factor (1 = fitted size).PreviewScale() float32
PreviewSrcPreviewSrc shows img in the preview instead of the image itself (Ant preview.src: a larger original behind a thumbnail).PreviewSrc(img image.Image) *ImageW
RadiusRadius rounds the corners.Radius(px float32) *ImageW
RotateRotate turns the open preview by deg degrees (clockwise, any angle; relative to its current rotation).Rotate(deg float64) *ImageW
ScaleStepScaleStep sets the zoom step of the preview buttons: each step scales by 1+step (Ant scaleStep 0.5; Element zoom-rate 1.2 = step 0.2).ScaleStep(step float32) *ImageW
ShowProgressShowProgress shows the "2 / 5" counter of a group preview (Ant always, Element show-progress, default false; the theme picks the default).ShowProgress(on bool) *ImageW
SizeSize fixes both sides.Size(wpx, hpx float32) *ImageW
WidthWidth / Height fix the box size in px (the other side follows the image aspect ratio when only one is set).Width(px float32) *ImageW

相关链接 ​

以 MIT 许可发布