1
0
mirror of https://github.com/ialley-workshop-open/uni-halo.git synced 2026-09-12 16:40:40 +08:00

refactor: 架构升级

This commit is contained in:
小莫唐尼
2026-08-31 07:58:23 +08:00
commit ba5b77568b
693 changed files with 118430 additions and 0 deletions
@@ -0,0 +1,222 @@
# uni.chooseImage - 选择图片示例
## 官方文档
参考官方文档:https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#chooseimage
## 概述
`uni.chooseImage` 用于从本地相册选择图片或使用相机拍照。
## 基础用法
```javascript
uni.chooseImage({
count: 1,
success: (res) => {
console.log('选择的图片', res.tempFilePaths)
}
})
```
## 完整示例
### 示例 1: 选择单张图片
```javascript
uni.chooseImage({
count: 1,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
console.log('选择的图片路径', res.tempFilePaths)
console.log('图片文件信息', res.tempFiles)
}
})
```
### 示例 2: 选择多张图片
```javascript
uni.chooseImage({
count: 9, // 最多选择9张
sizeType: ['compressed'], // 只选择压缩图
sourceType: ['album'], // 只从相册选择
success: (res) => {
console.log('选择了', res.tempFilePaths.length, '张图片')
res.tempFilePaths.forEach((path, index) => {
console.log(`图片${index + 1}:`, path)
})
}
})
```
### 示例 3: 在页面中使用
```vue
<template>
<view class="container">
<button @click="chooseImage">选择图片</button>
<view class="image-list">
<image
v-for="(item, index) in imageList"
:key="index"
:src="item"
mode="aspectFill"
class="image-item"
@click="previewImage(index)"
></image>
</view>
</view>
</template>
<script>
export default {
data() {
return {
imageList: []
}
},
methods: {
chooseImage() {
uni.chooseImage({
count: 9,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
this.imageList = res.tempFilePaths
uni.showToast({
title: `选择了${res.tempFilePaths.length}张图片`,
icon: 'success'
})
},
fail: (err) => {
uni.showToast({
title: '选择图片失败',
icon: 'none'
})
}
})
},
previewImage(index) {
uni.previewImage({
current: index,
urls: this.imageList
})
}
}
}
</script>
<style>
.image-list {
display: flex;
flex-wrap: wrap;
margin-top: 20px;
}
.image-item {
width: 200rpx;
height: 200rpx;
margin: 10rpx;
border-radius: 8rpx;
}
</style>
```
### 示例 4: 上传图片
```javascript
uni.chooseImage({
count: 1,
sizeType: ['compressed'],
sourceType: ['album', 'camera'],
success: (res) => {
const tempFilePath = res.tempFilePaths[0]
// 上传图片
uni.uploadFile({
url: 'https://api.example.com/upload',
filePath: tempFilePath,
name: 'file',
success: (uploadRes) => {
const data = JSON.parse(uploadRes.data)
console.log('上传成功', data.url)
uni.showToast({
title: '上传成功',
icon: 'success'
})
},
fail: (err) => {
console.error('上传失败', err)
uni.showToast({
title: '上传失败',
icon: 'none'
})
}
})
}
})
```
### 示例 5: 获取图片信息
```javascript
uni.chooseImage({
count: 1,
success: (res) => {
const tempFilePath = res.tempFilePaths[0]
// 获取图片信息
uni.getImageInfo({
src: tempFilePath,
success: (imageInfo) => {
console.log('图片宽度', imageInfo.width)
console.log('图片高度', imageInfo.height)
console.log('图片路径', imageInfo.path)
}
})
}
})
```
## 参数说明
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| count | Number | 否 | 最多可以选择的图片张数,默认 9 |
| sizeType | Array | 否 | 所选的图片的尺寸,可选值:original(原图)、compressed(压缩图) |
| sourceType | Array | 否 | 选择图片的来源,可选值:album(相册)、camera(相机) |
## 返回值
| 参数名 | 类型 | 说明 |
|--------|------|------|
| tempFilePaths | Array | 图片的本地文件路径列表 |
| tempFiles | Array | 图片的本地文件列表,每个文件包含 path 和 size |
## 平台兼容性
| 平台 | 支持情况 |
|------|---------|
| H5 | ✅ |
| 微信小程序 | ✅ |
| 支付宝小程序 | ✅ |
| 百度小程序 | ✅ |
| 字节跳动小程序 | ✅ |
| QQ 小程序 | ✅ |
| 快手小程序 | ✅ |
| App | ✅ |
| 快应用 | ✅ |
## 注意事项
1. 选择的图片是临时文件,需要上传到服务器才能永久保存
2. 临时文件路径在不同平台格式可能不同
3. 选择图片需要用户授权,首次使用会弹出授权提示
4. 建议使用压缩图以节省存储空间
## 参考资源
- **官方文档**: https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#chooseimage
- **预览图片**: https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#previewimage
- **上传文件**: https://doc.dcloud.net.cn/uni-app-x/api/request/network-file.html#uploadfile
@@ -0,0 +1,232 @@
# uni.chooseMedia - 选择媒体文件示例
## 官方文档
参考官方文档:https://doc.dcloud.net.cn/uni-app-x/api/media/video.html#choosemedia
## 概述
`uni.chooseMedia` 用于从本地相册选择图片或视频,或者使用相机拍摄图片或视频。
## 基础用法
```javascript
uni.chooseMedia({
count: 9,
success: (res) => {
console.log('选择的文件', res.tempFiles)
}
})
```
## 完整示例
### 示例 1: 选择图片和视频
```javascript
uni.chooseMedia({
count: 9,
mediaType: ['image', 'video'],
sourceType: ['album', 'camera'],
success: (res) => {
console.log('选择的文件', res.tempFiles)
res.tempFiles.forEach((file, index) => {
console.log(`文件${index + 1}:`, file.tempFilePath)
if (file.fileType === 'image') {
console.log('图片大小', file.size)
} else if (file.fileType === 'video') {
console.log('视频时长', file.duration)
}
})
}
})
```
### 示例 2: 只选择图片
```javascript
uni.chooseMedia({
count: 9,
mediaType: ['image'],
sourceType: ['album', 'camera'],
sizeType: ['original', 'compressed'],
success: (res) => {
console.log('选择的图片', res.tempFiles)
}
})
```
### 示例 3: 只选择视频
```javascript
uni.chooseMedia({
count: 1,
mediaType: ['video'],
sourceType: ['album', 'camera'],
maxDuration: 60,
camera: 'back',
success: (res) => {
console.log('选择的视频', res.tempFiles)
}
})
```
### 示例 4: 在页面中使用
```vue
<template>
<view class="container">
<button @click="chooseMedia">选择媒体文件</button>
<view class="media-list">
<view
v-for="(item, index) in mediaList"
:key="index"
class="media-item"
>
<image
v-if="item.fileType === 'image'"
:src="item.tempFilePath"
mode="aspectFill"
class="media-preview"
></image>
<video
v-else-if="item.fileType === 'video'"
:src="item.tempFilePath"
controls
class="media-preview"
></video>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
mediaList: []
}
},
methods: {
chooseMedia() {
uni.chooseMedia({
count: 9,
mediaType: ['image', 'video'],
sourceType: ['album', 'camera'],
success: (res) => {
this.mediaList = res.tempFiles
uni.showToast({
title: `选择了${res.tempFiles.length}个文件`,
icon: 'success'
})
},
fail: (err) => {
console.error('选择失败', err)
}
})
}
}
}
</script>
<style>
.media-list {
display: flex;
flex-wrap: wrap;
margin-top: 20px;
}
.media-item {
width: 200rpx;
height: 200rpx;
margin: 10rpx;
border-radius: 8rpx;
overflow: hidden;
}
.media-preview {
width: 100%;
height: 100%;
}
</style>
```
### 示例 5: 上传媒体文件
```javascript
uni.chooseMedia({
count: 9,
mediaType: ['image', 'video'],
success: (res) => {
const files = res.tempFiles
let uploadCount = 0
files.forEach((file, index) => {
uni.uploadFile({
url: 'https://api.example.com/upload',
filePath: file.tempFilePath,
name: 'file',
formData: {
fileType: file.fileType,
size: file.size
},
success: (uploadRes) => {
uploadCount++
console.log(`文件${index + 1}上传成功`)
if (uploadCount === files.length) {
uni.showToast({
title: '全部上传成功',
icon: 'success'
})
}
},
fail: (err) => {
console.error(`文件${index + 1}上传失败`, err)
}
})
})
}
})
```
## 参数说明
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| count | Number | 否 | 最多可以选择的文件个数,默认 9 |
| mediaType | Array | 否 | 文件类型,可选值:image、video |
| sourceType | Array | 否 | 选择文件来源,可选值:album、camera |
| maxDuration | Number | 否 | 拍摄视频最长拍摄时间,单位秒 |
| camera | String | 否 | 使用前置或后置摄像头,可选值:back、front |
## 返回值
| 参数名 | 类型 | 说明 |
|--------|------|------|
| tempFiles | Array | 选中的文件列表,每个文件包含 tempFilePath、size、fileType 等 |
## 平台兼容性
| 平台 | 支持情况 |
|------|---------|
| H5 | ✅ |
| 微信小程序 | ✅ |
| 支付宝小程序 | ✅ |
| 百度小程序 | ✅ |
| 字节跳动小程序 | ✅ |
| QQ 小程序 | ✅ |
| 快手小程序 | ✅ |
| App | ✅ |
| 快应用 | ✅ |
## 注意事项
1. `mediaType` 可以同时选择图片和视频
2. `count` 表示最多可以选择的文件个数
3. 返回的文件包含 `fileType` 字段,用于区分图片和视频
4. 建议根据实际需求设置 `maxDuration` 限制视频时长
## 参考资源
- **官方文档**: https://doc.dcloud.net.cn/uni-app-x/api/media/video.html#choosemedia
- **选择图片**: https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#chooseimage
- **选择视频**: https://doc.dcloud.net.cn/uni-app-x/api/media/video.html#choosevideo
@@ -0,0 +1,233 @@
# uni.chooseVideo - 选择视频示例
## 官方文档
参考官方文档:https://doc.dcloud.net.cn/uni-app-x/api/media/video.html#choosevideo
## 概述
`uni.chooseVideo` 用于从本地相册选择视频或使用相机拍摄视频。
## 基础用法
```javascript
uni.chooseVideo({
success: (res) => {
console.log('选择的视频', res.tempFilePath)
}
})
```
## 完整示例
### 示例 1: 选择视频
```javascript
uni.chooseVideo({
sourceType: ['album', 'camera'],
maxDuration: 60,
camera: 'back',
success: (res) => {
console.log('视频路径', res.tempFilePath)
console.log('视频时长', res.duration, '秒')
console.log('视频大小', res.size, '字节')
console.log('视频高度', res.height)
console.log('视频宽度', res.width)
}
})
```
### 示例 2: 拍摄视频
```javascript
uni.chooseVideo({
sourceType: ['camera'],
maxDuration: 30,
camera: 'back',
success: (res) => {
console.log('拍摄的视频', res.tempFilePath)
// 可以预览或上传视频
}
})
```
### 示例 3: 在页面中使用
```vue
<template>
<view class="container">
<button @click="chooseVideo">选择视频</button>
<button @click="recordVideo">拍摄视频</button>
<video
v-if="videoSrc"
:src="videoSrc"
controls
class="video-player"
></video>
</view>
</template>
<script>
export default {
data() {
return {
videoSrc: ''
}
},
methods: {
chooseVideo() {
uni.chooseVideo({
sourceType: ['album'],
maxDuration: 60,
success: (res) => {
this.videoSrc = res.tempFilePath
uni.showToast({
title: '选择成功',
icon: 'success'
})
},
fail: (err) => {
uni.showToast({
title: '选择失败',
icon: 'none'
})
}
})
},
recordVideo() {
uni.chooseVideo({
sourceType: ['camera'],
maxDuration: 30,
camera: 'back',
success: (res) => {
this.videoSrc = res.tempFilePath
uni.showToast({
title: '拍摄成功',
icon: 'success'
})
}
})
}
}
}
</script>
<style>
.video-player {
width: 100%;
height: 400px;
margin-top: 20px;
}
</style>
```
### 示例 4: 上传视频
```javascript
uni.chooseVideo({
sourceType: ['album', 'camera'],
maxDuration: 60,
success: (res) => {
const tempFilePath = res.tempFilePath
// 上传视频
uni.uploadFile({
url: 'https://api.example.com/upload-video',
filePath: tempFilePath,
name: 'video',
formData: {
duration: res.duration,
size: res.size
},
success: (uploadRes) => {
const data = JSON.parse(uploadRes.data)
console.log('上传成功', data.url)
uni.showToast({
title: '上传成功',
icon: 'success'
})
},
fail: (err) => {
console.error('上传失败', err)
}
})
}
})
```
### 示例 5: 限制视频时长和大小
```javascript
uni.chooseVideo({
sourceType: ['album', 'camera'],
maxDuration: 30, // 最长30秒
success: (res) => {
// 检查视频大小(例如限制为50MB)
const maxSize = 50 * 1024 * 1024 // 50MB
if (res.size > maxSize) {
uni.showModal({
title: '提示',
content: '视频文件过大,请选择小于50MB的视频',
showCancel: false
})
return
}
// 检查视频时长
if (res.duration > 30) {
uni.showModal({
title: '提示',
content: '视频时长不能超过30秒',
showCancel: false
})
return
}
console.log('视频符合要求', res)
}
})
```
## 参数说明
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| sourceType | Array | 否 | 选择视频的来源,可选值:album(相册)、camera(相机) |
| maxDuration | Number | 否 | 拍摄视频最长拍摄时间,单位秒 |
| camera | String | 否 | 使用前置或后置摄像头,可选值:back、front |
## 返回值
| 参数名 | 类型 | 说明 |
|--------|------|------|
| tempFilePath | String | 选定视频的临时文件路径 |
| duration | Number | 选定视频的时间长度,单位秒 |
| size | Number | 选定视频的数据量大小,单位字节 |
| width | Number | 选定视频的宽度,单位 px |
| height | Number | 选定视频的高度,单位 px |
## 平台兼容性
| 平台 | 支持情况 |
|------|---------|
| H5 | ✅ |
| 微信小程序 | ✅ |
| 支付宝小程序 | ✅ |
| 百度小程序 | ✅ |
| 字节跳动小程序 | ✅ |
| QQ 小程序 | ✅ |
| 快手小程序 | ✅ |
| App | ✅ |
| 快应用 | ✅ |
## 注意事项
1. 选择的视频是临时文件,需要上传到服务器才能永久保存
2. `maxDuration` 用于限制拍摄时长
3. 可以通过 `size` 检查视频文件大小
4. 建议在拍摄前提示用户视频时长限制
## 参考资源
- **官方文档**: https://doc.dcloud.net.cn/uni-app-x/api/media/video.html#choosevideo
- **上传文件**: https://doc.dcloud.net.cn/uni-app-x/api/request/network-file.html#uploadfile
@@ -0,0 +1,209 @@
# uni.getImageInfo - 获取图片信息示例
## 官方文档
参考官方文档:https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#getimageinfo
## 概述
`uni.getImageInfo` 用于获取图片信息,包括宽度、高度、路径等。
## 基础用法
```javascript
uni.getImageInfo({
src: 'https://example.com/image.jpg',
success: (res) => {
console.log('图片宽度', res.width)
console.log('图片高度', res.height)
}
})
```
## 完整示例
### 示例 1: 获取网络图片信息
```javascript
uni.getImageInfo({
src: 'https://example.com/image.jpg',
success: (res) => {
console.log('图片宽度', res.width)
console.log('图片高度', res.height)
console.log('图片路径', res.path)
},
fail: (err) => {
console.error('获取失败', err)
}
})
```
### 示例 2: 获取本地图片信息
```javascript
// 先选择图片
uni.chooseImage({
count: 1,
success: (res) => {
const tempFilePath = res.tempFilePaths[0]
// 获取图片信息
uni.getImageInfo({
src: tempFilePath,
success: (imageInfo) => {
console.log('图片信息', imageInfo)
// { width: 800, height: 600, path: '...' }
}
})
}
})
```
### 示例 3: 在页面中使用
```vue
<template>
<view class="container">
<button @click="selectAndGetInfo">选择图片并获取信息</button>
<view v-if="imageInfo" class="info">
<text>宽度{{ imageInfo.width }}px</text>
<text>高度{{ imageInfo.height }}px</text>
<image :src="imageInfo.path" mode="aspectFit" class="preview"></image>
</view>
</view>
</template>
<script>
export default {
data() {
return {
imageInfo: null
}
},
methods: {
selectAndGetInfo() {
uni.chooseImage({
count: 1,
success: (res) => {
const tempFilePath = res.tempFilePaths[0]
uni.getImageInfo({
src: tempFilePath,
success: (imageInfo) => {
this.imageInfo = imageInfo
},
fail: (err) => {
uni.showToast({
title: '获取图片信息失败',
icon: 'none'
})
}
})
}
})
}
}
}
</script>
<style>
.info {
margin-top: 20px;
padding: 20px;
}
.preview {
width: 100%;
max-height: 400px;
margin-top: 20px;
}
</style>
```
### 示例 4: 计算图片宽高比
```javascript
uni.getImageInfo({
src: 'https://example.com/image.jpg',
success: (res) => {
const aspectRatio = res.width / res.height
console.log('宽高比', aspectRatio)
// 根据宽高比调整显示
if (aspectRatio > 1) {
console.log('横向图片')
} else {
console.log('纵向图片')
}
}
})
```
### 示例 5: 验证图片尺寸
```javascript
function validateImageSize(imagePath, minWidth, minHeight) {
return new Promise((resolve, reject) => {
uni.getImageInfo({
src: imagePath,
success: (res) => {
if (res.width >= minWidth && res.height >= minHeight) {
resolve(res)
} else {
reject(new Error(`图片尺寸不符合要求,需要至少 ${minWidth}x${minHeight}`))
}
},
fail: (err) => {
reject(err)
}
})
})
}
// 使用
validateImageSize('https://example.com/image.jpg', 800, 600)
.then(info => {
console.log('图片符合要求', info)
})
.catch(err => {
console.error('验证失败', err)
})
```
## 参数说明
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| src | String | 是 | 图片的路径,可以是相对路径、临时文件路径或网络图片路径 |
## 返回值
| 参数名 | 类型 | 说明 |
|--------|------|------|
| width | Number | 图片宽度,单位 px |
| height | Number | 图片高度,单位 px |
| path | String | 返回图片的本地路径 |
## 平台兼容性
| 平台 | 支持情况 |
|------|---------|
| H5 | ✅ |
| 微信小程序 | ✅ |
| 支付宝小程序 | ✅ |
| 百度小程序 | ✅ |
| 字节跳动小程序 | ✅ |
| QQ 小程序 | ✅ |
| 快手小程序 | ✅ |
| App | ✅ |
| 快应用 | ✅ |
## 注意事项
1. 网络图片需要配置合法域名
2. 本地图片路径需要使用 `/static/` 开头或临时文件路径
3. 获取图片信息是异步操作
4. 可以用于验证图片尺寸、计算宽高比等
## 参考资源
- **官方文档**: https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#getimageinfo
- **选择图片**: https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#chooseimage
@@ -0,0 +1,234 @@
# uni.previewImage - 预览图片示例
## 官方文档
参考官方文档:https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#previewimage
## 概述
`uni.previewImage` 用于预览图片,支持缩放、滑动查看多张图片。
## 基础用法
```javascript
uni.previewImage({
urls: ['https://example.com/image1.jpg'],
current: 0
})
```
## 完整示例
### 示例 1: 预览单张图片
```javascript
uni.previewImage({
urls: ['https://example.com/image1.jpg'],
current: 'https://example.com/image1.jpg'
})
```
### 示例 2: 预览多张图片
```javascript
const imageList = [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg'
]
uni.previewImage({
urls: imageList,
current: 0 // 从第一张开始
})
```
### 示例 3: 在页面中使用
```vue
<template>
<view class="container">
<view class="image-list">
<image
v-for="(item, index) in imageList"
:key="index"
:src="item"
mode="aspectFill"
class="image-item"
@click="previewImage(index)"
></image>
</view>
</view>
</template>
<script>
export default {
data() {
return {
imageList: [
'https://example.com/image1.jpg',
'https://example.com/image2.jpg',
'https://example.com/image3.jpg'
]
}
},
methods: {
previewImage(index) {
uni.previewImage({
urls: this.imageList,
current: index
})
}
}
}
</script>
<style>
.image-list {
display: flex;
flex-wrap: wrap;
}
.image-item {
width: 200rpx;
height: 200rpx;
margin: 10rpx;
border-radius: 8rpx;
}
</style>
```
### 示例 4: 从选择图片到预览
```vue
<template>
<view class="container">
<button @click="chooseAndPreview">选择并预览图片</button>
<view class="image-list">
<image
v-for="(item, index) in selectedImages"
:key="index"
:src="item"
mode="aspectFill"
class="image-item"
@click="previewSelected(index)"
></image>
</view>
</view>
</template>
<script>
export default {
data() {
return {
selectedImages: []
}
},
methods: {
chooseAndPreview() {
uni.chooseImage({
count: 9,
success: (res) => {
this.selectedImages = res.tempFilePaths
// 预览第一张
if (res.tempFilePaths.length > 0) {
uni.previewImage({
urls: res.tempFilePaths,
current: 0
})
}
}
})
},
previewSelected(index) {
uni.previewImage({
urls: this.selectedImages,
current: index
})
}
}
}
</script>
```
### 示例 5: 长按保存图片
```vue
<template>
<view class="container">
<image
:src="imageUrl"
mode="aspectFit"
@longpress="saveImage"
class="preview-image"
></image>
</view>
</template>
<script>
export default {
data() {
return {
imageUrl: 'https://example.com/image.jpg'
}
},
methods: {
saveImage() {
uni.showActionSheet({
itemList: ['保存图片'],
success: (res) => {
if (res.tapIndex === 0) {
uni.downloadFile({
url: this.imageUrl,
success: (downloadRes) => {
uni.saveImageToPhotosAlbum({
filePath: downloadRes.tempFilePath,
success: () => {
uni.showToast({
title: '保存成功',
icon: 'success'
})
}
})
}
})
}
}
})
}
}
}
</script>
```
## 参数说明
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| urls | Array | 是 | 需要预览的图片 http 链接列表 |
| current | String/Number | 否 | 当前显示图片的索引或链接 |
## 平台兼容性
| 平台 | 支持情况 |
|------|---------|
| H5 | ✅ |
| 微信小程序 | ✅ |
| 支付宝小程序 | ✅ |
| 百度小程序 | ✅ |
| 字节跳动小程序 | ✅ |
| QQ 小程序 | ✅ |
| 快手小程序 | ✅ |
| App | ✅ |
| 快应用 | ✅ |
## 注意事项
1. `urls` 必须是网络图片或已下载的本地路径
2. `current` 可以是索引(Number)或图片链接(String
3. 预览时支持手势缩放和滑动切换
4. 建议使用网络图片时确保图片可访问
## 参考资源
- **官方文档**: https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#previewimage
- **选择图片**: https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#chooseimage
@@ -0,0 +1,274 @@
# uni.saveImageToPhotosAlbum - 保存图片到相册示例
## 官方文档
参考官方文档:https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#saveimagetophotosalbum
## 概述
`uni.saveImageToPhotosAlbum` 用于保存图片到系统相册。
## 基础用法
```javascript
uni.saveImageToPhotosAlbum({
filePath: '/tmp/image.jpg',
success: () => {
console.log('保存成功')
}
})
```
## 完整示例
### 示例 1: 保存网络图片
```javascript
// 先下载图片
uni.downloadFile({
url: 'https://example.com/image.jpg',
success: (res) => {
if (res.statusCode === 200) {
// 保存到相册
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
uni.showToast({
title: '保存成功',
icon: 'success'
})
},
fail: (err) => {
console.error('保存失败', err)
if (err.errMsg.includes('auth deny')) {
uni.showModal({
title: '提示',
content: '需要相册权限才能保存图片',
showCancel: false
})
}
}
})
}
}
})
```
### 示例 2: 保存选择的图片
```javascript
uni.chooseImage({
count: 1,
success: (res) => {
const tempFilePath = res.tempFilePaths[0]
uni.saveImageToPhotosAlbum({
filePath: tempFilePath,
success: () => {
uni.showToast({
title: '保存成功',
icon: 'success'
})
},
fail: (err) => {
console.error('保存失败', err)
}
})
}
})
```
### 示例 3: 在页面中使用
```vue
<template>
<view class="container">
<image :src="imageUrl" mode="aspectFit" class="preview-image"></image>
<button @click="saveImage">保存图片</button>
</view>
</template>
<script>
export default {
data() {
return {
imageUrl: 'https://example.com/image.jpg'
}
},
methods: {
saveImage() {
// 先下载图片
uni.downloadFile({
url: this.imageUrl,
success: (res) => {
if (res.statusCode === 200) {
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
uni.showToast({
title: '保存成功',
icon: 'success'
})
},
fail: (err) => {
if (err.errMsg.includes('auth deny')) {
uni.showModal({
title: '提示',
content: '需要相册权限,请在设置中开启',
success: (modalRes) => {
if (modalRes.confirm) {
uni.openSetting()
}
}
})
} else {
uni.showToast({
title: '保存失败',
icon: 'none'
})
}
}
})
}
}
})
}
}
}
</script>
```
### 示例 4: 检查权限
```javascript
function saveImageWithPermission(filePath) {
// 先检查权限
uni.getSetting({
success: (res) => {
if (res.authSetting['scope.writePhotosAlbum']) {
// 已授权,直接保存
saveImage(filePath)
} else {
// 请求授权
uni.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
saveImage(filePath)
},
fail: () => {
uni.showModal({
title: '提示',
content: '需要相册权限才能保存图片',
success: (modalRes) => {
if (modalRes.confirm) {
uni.openSetting()
}
}
})
}
})
}
}
})
}
function saveImage(filePath) {
uni.saveImageToPhotosAlbum({
filePath: filePath,
success: () => {
uni.showToast({
title: '保存成功',
icon: 'success'
})
}
})
}
```
### 示例 5: 长按保存图片
```vue
<template>
<view class="container">
<image
:src="imageUrl"
mode="aspectFit"
@longpress="handleLongPress"
class="preview-image"
></image>
</view>
</template>
<script>
export default {
data() {
return {
imageUrl: 'https://example.com/image.jpg'
}
},
methods: {
handleLongPress() {
uni.showActionSheet({
itemList: ['保存图片'],
success: (res) => {
if (res.tapIndex === 0) {
this.saveImage()
}
}
})
},
saveImage() {
uni.downloadFile({
url: this.imageUrl,
success: (res) => {
if (res.statusCode === 200) {
uni.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: () => {
uni.showToast({
title: '保存成功',
icon: 'success'
})
}
})
}
}
})
}
}
}
</script>
```
## 参数说明
| 参数名 | 类型 | 必填 | 说明 |
|--------|------|------|------|
| filePath | String | 是 | 图片文件路径,可以是临时文件路径或永久文件路径 |
## 平台兼容性
| 平台 | 支持情况 |
|------|---------|
| H5 | ✅ |
| 微信小程序 | ✅ |
| 支付宝小程序 | ✅ |
| 百度小程序 | ✅ |
| 字节跳动小程序 | ✅ |
| QQ 小程序 | ✅ |
| 快手小程序 | ✅ |
| App | ✅ |
| 快应用 | ✅ |
## 注意事项
1. 需要用户授权相册权限
2. 网络图片需要先下载到本地
3. 如果权限被拒绝,可以引导用户到设置中开启
4. 建议在保存前检查权限状态
## 参考资源
- **官方文档**: https://doc.dcloud.net.cn/uni-app-x/api/media/image.html#saveimagetophotosalbum
- **下载文件**: https://doc.dcloud.net.cn/uni-app-x/api/request/network-file.html#downloadfile
- **授权**: https://doc.dcloud.net.cn/uni-app-x/api/other/authorize.html#authorize