一、base64图片保存相册功能
提示api:that.$refs.uToast.show用的是uview2.0的toast,可以根据具体引入的ui库去更换;
saveBase64ImageToPhotosAlbum() {
let that = this
var timestamp = new Date().getTime();
let base64 = this.userInfo.jumpAddFriendQr.replace(/^data:image\/\w+;base64,/, ""); //去掉data:image/png;base64,
let filePath = wx.env.USER_DATA_PATH + `/addFriends_${timestamp}_qrcode.png`;
uni.showLoading({
title: '保存中',
mask: true
})
uni.getFileSystemManager().writeFile({
filePath: filePath, //创建一个临时文件名
data: base64, //写入的文本或二进制数据
encoding: 'base64', //写入当前文件的字符编码
success: res => {
uni.saveImageToPhotosAlbum({
filePath: filePath,
success: function(res2) {
uni.hideLoading();
that.$refs.uToast.show({
type: 'success',
message: "保存成功~",
duration: 1200,
})
},
fail: function(err) {
uni.hideLoading();
console.log(err.errMsg);
that.$refs.uToast.show({
type: 'error',
message: `保存失败`,
duration: 1200,
})
}
})
},
fail: err => {
uni.hideLoading();
console.log(err)
that.$refs.uToast.show({
type: 'error',
message: `创建文件失败`,
duration: 1200,
})
}
})
},
saveQRcodeFn() {//保存图片到相册按钮的点击事件函数
let that = this
uni.getSetting({
success(res) {
console.log(res);
if (!res.authSetting['scope.writePhotosAlbum']) {
uni.authorize({
scope: 'scope.writePhotosAlbum',
success(res) {
that.saveBase64ImageToPhotosAlbum()
},
fail() {
uni.showModal({
content: '请允许相册权限,拒绝将无法正常保存图片',
showCancel: false,
success() {
uni.openSetting({
success(settingdata) {
if (settingdata.authSetting['scope.writePhotosAlbum']) {} else {
console.log('获取权限失败')
that.$refs.uToast.show({
type: 'error',
message: `获取权限失败`,
duration: 1200,
})
}
}
})
}
})
}
})
} else {
that.saveBase64ImageToPhotosAlbum()
}
}
})
},
二、转发分享base64图片给微信好友功能
该功能在微信开发者工具中调试的时候会一直报错,真机是没问题的,可能是编译器的bug。
文章来源:https://www.toymoban.com/news/detail-667423.html
sharePic() {//分享图片给好友按钮的点击事件函数
let that = this
this.base64ToFilePath(this.userInfo.jumpAddFriendQr, (filePath) => {
console.log(filePath);
wx.showShareImageMenu({ //分享给朋友
path: filePath,
success: (res) => {
console.log("分享成功:", res);
},
fail: (err) => {
console.log("分享取消:", err);
},
})
})
},
base64ToFilePath(base64data, fun) {//封装的base64转换成临时文件路径的函数
const base64 = base64data; //base64格式图片
const time = new Date().getTime();
const imgPath = wx.env.USER_DATA_PATH + "/addFriends" + time + "share_qrcode" + ".png"; //去掉data:image/png;base64,如果图片字符串不含要清空的前缀,可以不执行下行代码.
const imageData = base64.replace(/^data:image\/\w+;base64,/, "");
const file = wx.getFileSystemManager();
file.writeFileSync(imgPath, imageData, "base64");
fun(imgPath);
},
其实整个wx.showShareImageMenu会拉起保存相册按钮,所以按道理也需要在调用这个api之前获取权限。这里封装了一下:参数fn就是获取权限后的函数文章来源地址https://www.toymoban.com/news/detail-667423.html
getAuth(fn) {
let that = this
uni.getSetting({
success(res) {
if (!res.authSetting['scope.writePhotosAlbum']) {
uni.authorize({
scope: 'scope.writePhotosAlbum',
success(res) {
fn()
},
fail() {
uni.showModal({
content: '请允许相册权限,拒绝将无法正常保存图片',
showCancel: false,
success() {
uni.openSetting({
success(settingdata) {
if (settingdata.authSetting['scope.writePhotosAlbum']) {} else {
console.log('获取权限失败')
that.$refs.uToast.show({
type: 'error',
message: `获取权限失败`,
duration: 1200,
})
}
}
})
}
})
}
})
} else {
fn()
}
}
})
},
到了这里,关于uniapp 微信小程序 实现 将base64图片保存相册和转发分享微信好友功能记录 直接cv就能用!!!!的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!