/** 本地图片转Base64*/
async function imgToBase64({ path }) {
try {
const result = await Taro.request({
url: path,
responseType: "arraybuffer"
});
if (result && result.statusCode !== 200) {
Taro.getLogManager().warn("图片获取失败", result);
throw new Error("图片获取失败。");
}
let base64 = Taro.arrayBufferToBase64(result.data);
base64 = "data:image/jpeg;base64," + base64;
return base64;
} catch (error) {
console.warn("=> utilssearch.ts error imgToBase64", error);
throw error;
}
}
/**
* 本地图片转Base64
* @param param.path 文件路径
* @returns Base64图片字符串
*/
function imgToBase64({ path }) {
let res = "";
try {
const base64 = Taro.getFileSystemManager().readFileSync(path, "base64");
if (base64) {
res = "data:image/jpeg;base64," + base64;
}
} catch (error) {
console.warn("=> utilssearch.ts error imgToBase64", error);
throw error;
} finally {
return res;
}
}
原文:https://www.cnblogs.com/KevinTseng/p/14722649.html