我这里做的是,用户拍照时的图片压缩(选择图片同理),直接上代码:
buttonclickTwo: function () {
var that = this
uni.showActionSheet({
itemList: ['拍照'],
itemColor: '',
success: function (res) {
if (!res.cancel) {
console.log(res.tapIndex);
if (res.tapIndex === 0) {
//拍照
uni.chooseImage({
count: 1,
sourceType: ['camera'],
success: (chooseImageRes) => {
uni.showLoading({
title: '加载中'
});
console.log('imgsss',chooseImageRes.tempFiles)
var imgPath = chooseImageRes.tempFiles[0].path
console.log(imgPath)
// 判断原图是否大于100k 压缩
if(chooseImageRes.tempFiles[0].size>=100000) {
// uniapp 压缩只支持jpg 故判断
if(imgPath.endsWith('png')) {
console.log('是png图片')
imgPath.replace(/png/g,'jpg')
}
uni.compressImage({
src: imgPath,
quality: 80,
success: res => {
console.log('压缩获得图片地址',res.tempFilePath)
imgPath = res.tempFilePath
//自己写的上传方法
that.uploadImg(imgPath)
}
})
}else {
//自己写的上传方法
that.uploadImg(imgPath)
}
}
});
}
}
},
fail: function (res) {
},
complete: function (res) {
},
});
},