Image 图片
展示图片并提供尺寸、裁剪、占位、失败回退和预览能力。
何时使用
- 头像、封面、缩略图和内容图片展示。
- 需要点击放大、旋转或浏览图片组时使用预览。
- 仅需图标时使用
icon包,避免加载图片。
在线体验
基本用法
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("示例图片")
}常见用法
裁剪与圆角
func crop(c *ui.Context, img image.Image) {
ui.Image(c, img).Size(240, 160).Fit("cover").Radius(8)
}加载和失败回退
func loading(c *ui.Context, img, fallback image.Image, pending, failed bool) {
ui.Image(c, img).Loading(pending).Failed(failed).Fallback(fallback).Alt("产品封面")
}预览组
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 入门。
API
签名与说明由 site/tools/compdoc 从 Go 源码和文档注释提取;点击名称跳到 Go API 参考 的对应条目。
入口函数
ui.Image
func Image(c *Context, img image.Image) *ImageWImage 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).
| 方法 | 说明 | 签名 |
|---|---|---|
A11yLabel | A11yLabel sets the accessible name (like Alt, without changing the Driver.Find label). | A11yLabel(s string) *ImageW |
Alt | Alt sets the accessible label (also used by Driver.Find). | Alt(s string) *ImageW |
CloseOnEscape | CloseOnEscape(false) keeps the preview open on Escape (Element close-on-press-escape, default true). | CloseOnEscape(on bool) *ImageW |
Failed | Failed shows the error fallback (broken picture / "加载失败"). | Failed(on bool) *ImageW |
Fallback | Fallback sets the image shown when loading failed (Ant `fallback`). | Fallback(img image.Image) *ImageW |
Fit | Fit sets object-fit: "fill" (default), "contain", "cover", "scale-down". | Fit(f string) *ImageW |
Height | Height(px float32) *ImageW | |
ID | ID returns the widget ID. | ID() ID |
Infinite | Infinite 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 |
Layout | Layout applies layout options. | Layout(o ...layout.Option) *ImageW |
Loading | Loading shows the placeholder instead of the image. | Loading(on bool) *ImageW |
MaskClosable | MaskClosable 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 |
MaxScale | MaxScale(k float32) *ImageW | |
MinScale | MinScale 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 |
Movable | Movable(false) stops dragging the preview image around (Ant preview.movable, default true). | Movable(on bool) *ImageW |
OpenPreview | OpenPreview opens the preview now (Ant preview.open); call it once per action, like Rotate. | OpenPreview() *ImageW |
Preview | Preview enables click-to-preview. | Preview(on bool) *ImageW |
PreviewAngle | PreviewAngle returns the preview rotation in degrees (clockwise). | PreviewAngle() float64 |
PreviewGroup | PreviewGroup previews imgs (this image is imgs[index]) with prev/next navigation and a counter. | PreviewGroup(imgs []image.Image, index int) *ImageW |
PreviewOpen | PreviewOpen reports whether the preview overlay is open. | PreviewOpen() bool |
PreviewScale | PreviewScale returns the preview zoom factor (1 = fitted size). | PreviewScale() float32 |
PreviewSrc | PreviewSrc shows img in the preview instead of the image itself (Ant preview.src: a larger original behind a thumbnail). | PreviewSrc(img image.Image) *ImageW |
Radius | Radius rounds the corners. | Radius(px float32) *ImageW |
Rotate | Rotate turns the open preview by deg degrees (clockwise, any angle; relative to its current rotation). | Rotate(deg float64) *ImageW |
ScaleStep | ScaleStep 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 |
ShowProgress | ShowProgress 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 |
Size | Size fixes both sides. | Size(wpx, hpx float32) *ImageW |
Width | Width / Height fix the box size in px (the other side follows the image aspect ratio when only one is set). | Width(px float32) *ImageW |