uniapp 图片相关(压缩、转base64)
// 压缩图片
export function compressImage(url,qa) {
return new Promise((resolve, reject) => {
const filenames = url.split('/')
const name = "_doc/upload/" + filenames[filenames.length-1];
plus.zip.compressImage(
{
src: url, //src: (String 类型 )压缩转换原始图片的路径
dst: name, //压缩转换目标图片的路径
quality: qa, //quality: (Number 类型 )压缩图片的质量.取值范围为1-100
overwrite: true, //overwrite: (Boolean 类型 )覆盖生成新文件
},
function (zip) {
//页面显示图片
resolve(zip);
},
function (error) {
reject(error);
}
);
});
}
// 缩小图片
export function scaleimg(url,filename,width,height) {
return new Promise((resolve, reject) => {
const filenames = url.split('/')
const name = "_doc/upload/" + filenames[filenames.length-1];
plus.zip.compressImage(
{
src: url, //src: (String 类型 )压缩转换原始图片的路径
dst: name, //压缩转换目标图片的路径
overwrite: true, //overwrite: (Boolean 类型 )覆盖生成新文件
width,
height,
},
function (zip) {
//页面显示图片
resolve(zip);
},
function (error) {
reject(error);
}
);
});
}
// 转base64
export function toBase64(url) {
return new Promise((resolve, reject) => {
//根据路径读取到文件
plus.io.resolveLocalFileSystemURL(url, function (entry) {
entry.file(function (file) {
let fileReader = new plus.io.FileReader();
fileReader.readAsDataURL(file);
fileReader.onloadend = function (e) {
const picUrl = e.target.result.toString();
resolve(picUrl);
};
});
});
});
}
本文来自博客园,作者:小虾米吖~,转载请注明原文链接:https://www.cnblogs.com/LindaBlog/p/18271254