uni-app 使用的uni.chooseMedia方法 iOS必现闪退
uni-app 使用的uni.chooseMedia方法 iOS必现闪退
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | 11 家庭版 25H2 | HBuilderX |
产品分类:uniapp/App
PC开发环境操作系统:Windows
HBuilderX类型:正式
HBuilderX版本号:4.84
手机系统:iOS
手机系统版本号:iOS 18
手机厂商:苹果
手机机型:iphone13
页面类型:vue
vue版本:vue3
打包方式:云端
App下载地址或H5网址:
https://www.pgyer.com/cangshenghao
示例代码:
const chooseAndUploadFiles = (
fileType: string | null,
maxCount: number,
isCover: boolean = false
) => {
// 如果已经有文件,只允许上传相同类型的文件
const mediaType: ('image' | 'video' | 'mix')[] =
fileType === 'image' ? ['image'] : fileType === 'video' ? ['video'] : ['mix']
uni.chooseMedia({
count: maxCount,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
mediaType: mediaType,
success: function (res) {
console.log(res, 'res')
const tempFilePaths = res.tempFiles
// 检查是否同时包含图片和视频
const hasImage = tempFilePaths.some((item) => item.fileType === 'image')
const hasVideo = tempFilePaths.some((item) => item.fileType === 'video')
if (hasImage && hasVideo) {
uni.showToast({ icon: 'none', title: '只能上传图片或视频中的一种' })
return
}
// 如果选择的是视频,限制只能上传一个
if (hasVideo && tempFilePaths.length > 1) {
uni.showToast({ icon: 'none', title: '视频只能上传一个' })
return
}
uni.showLoading({
title: '上传中'
})
const uploadPromises = tempFilePaths.map((file) => {
return new Promise((resolve, reject) => {
uni.uploadFile({
url: hasVideo ? BASE_Video_UPLOADURL : BASE_UPLOADURL,
filePath: file.tempFilePath,
name: 'file',
header: {
Authorization: usePersistCommonStore().token
},
success: (uploadFileRes) => {
try {
const data = JSON.parse(uploadFileRes.data)
console.log(data, 'data')
if (isCover) {
formData.value.coverUrl = data.data
}
if (file.fileType === 'image' && !isCover) {
formData.value.images.push(data.data)
} else if (file.fileType === 'video' && !isCover) {
formData.value.videoUrl = data?.data?.url || ''
videoCover.value = data?.data?.capture || ''
if (!formData.value.coverUrl) {
formData.value.coverUrl = data?.data?.capture || ''
}
}
resolve(data)
} catch (error) {
reject(error)
}
},
fail: (error) => {
console.error('上传失败详情:', error)
// 记录更详细的错误信息,包括网络状态、文件大小等
console.log('上传文件信息:', {
fileType: file.fileType,
size: file.size,
path: file.tempFilePath
})
console.log('上传配置:', {
url: hasVideo ? BASE_Video_UPLOADURL : BASE_UPLOADURL,
token: !!usePersistCommonStore().token
})
reject(error)
},
complete: () => {
// 由于Promise.all会等待所有上传完成,这里不需要单独处理loading
}
})
})
// 等待所有文件上传完成
Promise.all(uploadPromises)
.then(() => {
uni.hideLoading()
})
.catch((error) => {
console.error('Promise.all 上传失败:', error)
uni.hideLoading()
uni.showToast({ icon: 'none', title: '您的网络环境不稳定或存在波动,请稍后再试。' })
})
}
操作步骤:
进入编辑信息页面,删除之前的图片,点击上传图片,点击拍摄,出现闪退 只有ios会出现
预期结果:
进入编辑信息页面,删除之前的图片,点击上传图片,点击拍摄,正常上传
实际结果:
进入编辑信息页面,删除之前的图片,点击上传图片,点击拍摄,出现闪退 只有ios会出现
bug描述:
使用的uni.chooseMedia方法,iOS必现闪退
更多关于uni-app 使用的uni.chooseMedia方法 iOS必现闪退的实战教程也可以访问 https://www.itying.com/category-93-b0.html
提供一个 可复现问题的简单示例吧
更多关于uni-app 使用的uni.chooseMedia方法 iOS必现闪退的实战教程也可以访问 https://www.itying.com/category-93-b0.html
整个代码吗
解决了,不能按照uni给的type类型使用mix
(property) UniNamespace.ChooseMediaOption.mediaType?: (“video” | “image” | “mix”)[] | undefined
文件类型
可选值:
‘image’: 只能拍摄图片或从相册选择图片;
‘video’: 只能拍摄视频或从相册选择视频;
‘mix’: 可同时选择图片和视频;
这是一个典型的iOS权限配置问题。在iOS系统中,使用uni.chooseMedia方法调用相机功能时,必须在manifest.json文件中正确配置相机权限声明。
检查您的manifest.json文件,确保包含以下配置:
{
"app-plus": {
"distribute": {
"ios": {
"permissions": {
"Camera": {
"desc": "需要访问相机以拍摄照片和视频"
},
"PhotoLibrary": {
"desc": "需要访问相册以选择照片和视频"
}
}
}
}
}
}
同时,在调用相机前建议先检查权限状态:
uni.authorize({
scope: 'scope.camera',
success: () => {
// 授权成功,调用chooseMedia
uni.chooseMedia({
// 参数配置
})
},
fail: () => {
uni.showModal({
content: '需要相机权限才能拍摄照片',
confirmText: '去设置',
success: (res) => {
if (res.confirm) {
uni.openSystemSettings()
}
}
})
}
})

