// 选择图片并自动转成base64编码(count:文件可选数,imgType:转base64时图片类型)
function chooseMedia(count) {
return new Promise((resolve, reject) => {
wx.chooseMedia({
count: count,
mediaType: ['image', 'video'],
sourceType: ['album', 'camera'],
maxDuration: 30,
camera: 'back',
success: (res) => {
console.log(res)
var tempFilePaths = res.tempFiles;
var base64ImgArr = [];
tempFilePaths.forEach((item) => {
var url=item.tempFilePath;
wx.getImageInfo({
src: url,
success(imageInfo) {
var imgType = imageInfo.type
wx.getFileSystemManager().readFile({
filePath: imageInfo.path, //选择图片返回的相对路径
encoding: "base64", //这个是很重要的
success: res => { //成功的回调
//返回base64格式
var base64Str = res.data
base64ImgArr.push(base64Str)
if (base64ImgArr.length == tempFilePaths.length) {
resolve(base64ImgArr)
}
},
fail: err => {
console.log(err)
reject(err)
}
})
}
})
})
},
fail: (err) => {
console.log(err)
}
})
})
}