168 lines
4.7 KiB
JavaScript
168 lines
4.7 KiB
JavaScript
|
const imgTools = {
|
|||
|
/**
|
|||
|
* @description 图片多选,并返回图片数组
|
|||
|
* @param {Number} count 1
|
|||
|
*/
|
|||
|
chooseImg(count=1) {
|
|||
|
return new Promise((resolve,reject)=>{
|
|||
|
let imgArr = [];
|
|||
|
uni.chooseImage({
|
|||
|
count:count,
|
|||
|
sizeType:['compressed'],
|
|||
|
sourceType:['album'],
|
|||
|
success: (res) => {
|
|||
|
let files = res.tempFilePaths
|
|||
|
files.forEach(item=>{
|
|||
|
imgArr.push(item);
|
|||
|
})
|
|||
|
resolve(imgArr);
|
|||
|
}
|
|||
|
})
|
|||
|
})
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 选择图片并压缩图片
|
|||
|
* @param {Number} cutQuality 80
|
|||
|
* @param {Number} cutWidth 200
|
|||
|
* @param {Number} cutWidth 200
|
|||
|
*/
|
|||
|
chooseCompressImg(cutQuality=80,cutWidth=200,cutHeight=200) {
|
|||
|
return new Promise((resolve, reject) => {
|
|||
|
uni.chooseImage({
|
|||
|
count:1,
|
|||
|
sourceType:['album','camera'],
|
|||
|
extension:['png'],//选择图片类型
|
|||
|
crop:{//图像裁剪参数,设置后 sizeType 失效
|
|||
|
quality:cutQuality,//1~100
|
|||
|
width:cutWidth,
|
|||
|
height:cutHeight,
|
|||
|
resize:false//是否将width和height作为裁剪保存图片真实的像素值。默认值为true。注:设置为false时在裁剪编辑界面显示图片的像素值,设置为true时不显示
|
|||
|
},
|
|||
|
success: (res) => {
|
|||
|
uni.compressImage({
|
|||
|
src: res.tempFilePaths[0],
|
|||
|
quality:100,//压缩质量,范围0~100,数值越小,质量越低,压缩率越高(仅对jpg有效)
|
|||
|
width:cutWidth,
|
|||
|
height:cutHeight,
|
|||
|
rotate:0,//旋转度数,范围0~360
|
|||
|
success: res => {
|
|||
|
resolve(res.tempFilePath)
|
|||
|
}
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
})
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 预览图片
|
|||
|
* @param {Number} current 0
|
|||
|
* @param {Array} imgArr []
|
|||
|
*/
|
|||
|
previewImg(current=0,imgArr=[]) {
|
|||
|
uni.previewImage({
|
|||
|
current:current,
|
|||
|
urls: imgArr,
|
|||
|
indicator:'number',//图片指示器样式,可取值:"default" - 底部圆点指示器; "number" - 顶部数字指示器; "none" - 不显示指示器。
|
|||
|
loop:false,//是否可循环预览,默认值为 false
|
|||
|
longPressActions: {
|
|||
|
itemList: ['发送给朋友', '保存图片', '收藏'],
|
|||
|
itemColor:'#000000',//按钮的文字颜色,字符串格式,默认为"#000000"
|
|||
|
success:(data)=> {
|
|||
|
// console.log('选中了第' + (data.tapIndex + 1) + '个按钮,第' + (data.index + 1) + '张图片');
|
|||
|
switch (data.tapIndex){
|
|||
|
case 0:
|
|||
|
// 发送给朋友
|
|||
|
break;
|
|||
|
case 1:
|
|||
|
// 保存图片
|
|||
|
this.saveImg(imgArr[data.index]);
|
|||
|
break;
|
|||
|
case 2:
|
|||
|
// 收藏
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
});
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 保存图片
|
|||
|
* @param {String} src
|
|||
|
*/
|
|||
|
saveImg(src) {
|
|||
|
uni.saveImageToPhotosAlbum({
|
|||
|
filePath: src,
|
|||
|
success: ()=> {}
|
|||
|
});
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 压缩图片
|
|||
|
* @param {String} imgsrc
|
|||
|
* @param {Number} quality 裁剪质量(1~100),默认:80
|
|||
|
* @param {Number} width 裁剪宽度,默认:不设置
|
|||
|
* @param {Number} height 裁剪宽度,默认:不设置
|
|||
|
* @param {Number} rotate 裁剪角度(0~360),默认:0
|
|||
|
*/
|
|||
|
compressImg(imgsrc, quality=80, width='', height='', rotate=0) {
|
|||
|
return new Promise((resolve, reject)=>{
|
|||
|
uni.compressImage({
|
|||
|
src: imgsrc,
|
|||
|
quality,//压缩质量,范围0~100,数值越小,质量越低,压缩率越高(仅对jpg有效)
|
|||
|
width,
|
|||
|
height,
|
|||
|
rotate,//旋转度数,范围0~360
|
|||
|
success: res => {
|
|||
|
resolve(res.tempFilePath);
|
|||
|
}
|
|||
|
})
|
|||
|
})
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 获取图片信息
|
|||
|
* @param {String} src
|
|||
|
*/
|
|||
|
getImgInfo(src) {
|
|||
|
return new Promise((resolve, reject)=>{
|
|||
|
uni.getImageInfo({
|
|||
|
src,
|
|||
|
success: (image)=> {
|
|||
|
resolve(image)
|
|||
|
}
|
|||
|
});
|
|||
|
})
|
|||
|
},
|
|||
|
/**
|
|||
|
* @description 选择并裁剪图片
|
|||
|
* @param {Number} cutQuality 裁剪质量(1~100),默认:100
|
|||
|
* @param {Number} cutWidth 裁剪宽度,默认:200
|
|||
|
* @param {Number} cutHeight 裁剪高度,默认:200
|
|||
|
* @param {Number} cutRotate 裁剪角度(0~360),默认:0
|
|||
|
*/
|
|||
|
cutImg(cutQuality=100,cutWidth=200,cutHeight=200, cutRotate=0) {
|
|||
|
return new Promise((resolve, reject)=>{
|
|||
|
uni.chooseImage({
|
|||
|
count:1,
|
|||
|
sourceType:['album','camera'],
|
|||
|
crop:{//图像裁剪参数,设置后 sizeType 失效
|
|||
|
quality:cutQuality,//1~100
|
|||
|
width:cutWidth,
|
|||
|
height:cutHeight,
|
|||
|
resize:false//是否将width和height作为裁剪保存图片真实的像素值。默认值为true。注:设置为false时在裁剪编辑界面显示图片的像素值,设置为true时不显示
|
|||
|
},
|
|||
|
success: (res) => {
|
|||
|
uni.compressImage({
|
|||
|
src: res.tempFilePaths[0],
|
|||
|
quality:100,//压缩质量,范围0~100,数值越小,质量越低,压缩率越高(仅对jpg有效)
|
|||
|
width:cutWidth,
|
|||
|
height:cutHeight,
|
|||
|
rotate:cutRotate,//旋转度数,范围0~360
|
|||
|
success: res => {
|
|||
|
resolve(res.tempFilePath)
|
|||
|
}
|
|||
|
})
|
|||
|
}
|
|||
|
})
|
|||
|
})
|
|||
|
}
|
|||
|
}
|
|||
|
export default imgTools;
|