简体中文
组件类型:UniImageElement
图片
| Web | 微信小程序 | Android | iOS | HarmonyOS | HarmonyOS(Vapor) |
|---|---|---|---|---|---|
| 4.0 | 4.41 | 3.9 | 4.11 | 4.61 | 5.0 |
| 名称 | 类型 | 默认值 | 兼容性 | 描述 | |||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| src | string(string.ImageURIString) | - | 图片资源地址 | ||||||||||||||||||||||||||||||||||||||||||||||
| mode | string | "scaleToFill" | 图片裁剪、缩放的模式 | ||||||||||||||||||||||||||||||||||||||||||||||
| |||||||||||||||||||||||||||||||||||||||||||||||||
| lazy-load | boolean | false | 图片懒加载。只针对page与scroll-view下的image有效。 安卓默认懒加载不支持修改 | ||||||||||||||||||||||||||||||||||||||||||||||
| fade-show | boolean | false | 图片显示动画效果 | ||||||||||||||||||||||||||||||||||||||||||||||
| webp | boolean | true | 是否支持 WebP 格式,web、app平台默认支持 WebP 格式,不支持本属性,无法关闭对 WebP 格式的解析。 | ||||||||||||||||||||||||||||||||||||||||||||||
| show-menu-by-longpress | boolean | false | 开启长按图片显示识别小程序码菜单 | ||||||||||||||||||||||||||||||||||||||||||||||
| draggable | boolean | false | 鼠标长按是否能拖动图片(仅H5平台) | ||||||||||||||||||||||||||||||||||||||||||||||
| flatten | boolean | false | 是否拍平组件 | ||||||||||||||||||||||||||||||||||||||||||||||
| @error | (event: UniImageErrorEvent) => void | - | 图片加载错误时触发,event.detail = { errMsg } | ||||||||||||||||||||||||||||||||||||||||||||||
| @load | (event: UniImageLoadEvent) => void | - | 图片加载完成时触发,event.detail = { width: '图片宽度px', height: '图片高度px' } | ||||||||||||||||||||||||||||||||||||||||||||||
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| detail | UniImageErrorEventDetail | 是 | - | - | |||||||||||||
| |||||||||||||||||
| 名称 | 类型 | 必填 | 默认值 | 兼容性 | 描述 | ||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| detail | UniImageLoadEventDetail | 是 | - | - | |||||||||||||||||||
| |||||||||||||||||||||||
如需其他图片格式,可自行开发uts组件插件或搜索插件市场,如
本地路径/static方式 由于uni-app/uni-app x编译时,只把/static目录下的静态资源copy到app中,所以src均需指向/static目录下。 其他目录的图片由于不会被打包进去,所以无法访问。 app平台文件路径会存在大小写敏感问题,为了有更好的兼容性,建议统一按大小写敏感原则处理 详情
本地绝对路径file:///方式
app-android平台形如file:///storage/emulated/0/Android/data/io.dcloud.uniappx/apps/__UNI__4517034/www/static/test-image/logo.png。
访问本应用内的资源时无需使用本方式,推荐使用/static方式。上述地址受包名、appid影响。
file:///方式一般用于download等公共目录。使用前需确保拥有相关权限。
支持网络路径
不可以嵌套组件
示例为hello uni-app x alpha分支,与最新HBuilderX Alpha版同步。与最新正式版同步的master分支示例另见
示例
<template>
<view style="flex: 1;">
<page-head :title="data.title"></page-head>
<scroll-view style="flex: 1">
<view class="uni-padding-wrap">
<!-- image样式大合集 -->
<text class="uni-title-text">image样式大合集</text>
<text class="uni-subtitle-text uni-common-mb">右边为拍平</text>
<view class="styled-image-row">
<image class="styled-image" mode="aspectFill" :src="data.imageSrc"></image>
<image class="styled-image" mode="aspectFill" :src="data.imageSrc" flatten></image>
</view>
<text class="uni-title-text">自定义组件:右边拍平</text>
<view class="styled-image-row">
<child></child>
<child flatten></child>
</view>
<!-- 原有的基础拍平测试 -->
<text class="uni-title-text uni-common-mt">基础(右边为拍平)</text>
<view class="uni-center image-bg">
<image class="image" :fade-show="true" mode="widthFix" :src="data.imageSrc" @error="error" @load="load"></image>
<image class="image" :fade-show="true" mode="widthFix" :src="data.imageSrc" @error="error" @load="load" flatten></image>
</view>
<button class="uni-btn" @tap="imageFormat">图片格式示例</button>
<button class="uni-btn" @tap="imageMode">图片缩放模式示例</button>
<button class="uni-btn" @tap="imagePath">图片路径示例</button>
<button class="uni-btn" @tap="imageLarge">大图示例</button>
<button class="uni-btn" @tap="imageLong">长图示例</button>
<button class="uni-btn" @tap="imageOrientation">图片方向修正示例</button>
<!-- #ifdef VUE3-VAPOR -->
<navigator url="/pages/template/2000-image/2000-image">
<button class="uni-btn">组件性能测试</button>
</navigator>
<!-- #endif -->
</view>
</scroll-view>
<view v-if="data.autoTest">
<image :src="data.setCookieImage"></image>
<image :src="data.verifyCookieImage" @error="error"></image>
</view>
</view>
</template>
<script setup lang="uts">
import Child from './child.uvue'
type DataType = {
title: string;
imageSrc: string.ImageURIString;
loadError: boolean;
autoTest: boolean;
setCookieImage: string;
verifyCookieImage: string;
eventLoad: UTSJSONObject | null;
eventError: UTSJSONObject | null;
}
// 使用reactive避免ref数据在自动化测试中无法访问
const data = reactive({
title: 'image',
imageSrc: "/static/test-image/logo.png",
loadError: false,
// 自动化测试
autoTest: false,
setCookieImage: "",
verifyCookieImage: "",
eventLoad: null,
eventError: null
} as DataType)
const error = (event: ImageErrorEvent) => {
data.loadError = true
console.log(event.type, event.detail);
if (data.autoTest) {
data.eventError = {
"tagName": event.target?.tagName,
"type": event.type,
// "errMsg": event.detail.errMsg
};
}
}
const load = (event: ImageLoadEvent) => {
console.log(event.type, event.detail);
if (data.autoTest) {
data.eventLoad = {
"tagName": event.target?.tagName,
"type": event.type,
"width": event.detail.width,
"height": event.detail.height
};
}
}
const imageFormat = () => {
uni.navigateTo({
url: '/pages/component/image/image-format'
});
}
const imageMode = () => {
uni.navigateTo({
url: '/pages/component/image/image-mode'
});
}
const imagePath = () => {
uni.navigateTo({
url: '/pages/component/image/image-path'
});
}
const imageLarge = () => {
uni.navigateTo({
url: '/pages/component/image/image-large'
});
}
const imageLong = () => {
uni.navigateTo({
url: '/pages/component/image/image-long'
});
}
const imageOrientation = () => {
uni.navigateTo({
url: '/pages/component/image/image-orientation'
});
}
defineExpose({
data,
error,
load,
imageFormat,
imageMode,
imagePath,
imageLarge,
imageLong
})
</script>
<style>
.styled-image-row {
flex-direction: row;
background: #fff;
justify-content: space-around;
height: 120px;
align-items: center;
}
/* image样式大合集 */
.styled-image {
width: 80px;
height: 80px;
margin: 5px;
padding: 5px;
border: 2px solid #ff6b6b;
border-radius: 10px;
background-color: #ffe0e0;
/* === 阴影 === */
box-shadow: 0 3px 6px rgba(255, 107, 107, 0.3);
/* === 可见性 === */
opacity: 0.98;
/* === 变换 === */
transform: rotate(45deg);
}
.image {
margin: 20px auto;
width: 100px;
}
.image-bg {
background: #FFFFFF;
}
</style>