断断续续的搞了两次,终于把这个功能实现了。
1.二维码的插件
2.转base64格式的工具(图像转换工具,可用于图像和base64的转换)
3.uniapp安装
// 以下路径需根据项目实际情况填写
import { pathToBase64, base64ToPath } from '../../js/image-tools/index.js'
4.pathToBase64
从图像路径转换为base64,uni-app、微信小程序和5+APP使用的路径不支持网络路径,如果是网络路径需要先使用下载API下载下来。
pathToBase64(path)
.then(base64 => {
console.log(base64)
})
.catch(error => {
console.error(error)
})
5.base64ToPath
将图像base64保存为文件,返回文件路径。
base64ToPath(base64)
.then(path => {
console.log(path)
})
.catch(error => {
console.error(error)
})
6.可以利用promise来串行和并行的执行多个任务
// 并行
Promise.all(paths.map(path => pathToBase64(path)))
.then(res => {
console.log(res)
// [base64, base64...]
})
.catch(error => {
console.error(error)
})
// 串行
paths.reduce((promise, path) => promise.then(res => pathToBase64(path).then(base64 => (res.push(base64), res))), Promise.resolve([]))
.then(res => {
console.log(res)
// [base64, base64...]
})
.catch(error => {
console.error(error)
})
7.我是将二维码的图片转成base64格式
methods: {
showfromchild(data图片的url){
var that=this;
pathToBase64(data)
.then(base64 => {
that.qrvalimg=base64;
})
.catch(error => {
console.error(error)
})
},
8.然后通过plus.nativeObj.Bitmap()这个函数分享base64格式的图片(代码有点low请见谅)
var bitmap = new plus.nativeObj.Bitmap();
bitmap.loadBase64Data(that.qrvalimg, function() {
bitmap.save("DCIM/a.jpg", {
overwrite: true
}, function(i) {
uni.hideLoading()
uni.share({
provider: "sinaweibo",
type: 0,
summary: '这是我的收款码',
imageUrl: i.target,
success: function(res) {
console.log("success:" + JSON.stringify(res));
},
fail: function(err) {
console.log("fail:" + JSON.stringify(err));
}
});
// console.log('保存图片成功:' + JSON.stringify(i));
}, function(e) {
console.log('保存图片失败:' + JSON.stringify(e));
});
}, function(e) {
console.log('绘制图片失败:' + JSON.stringify(e));
});