咸虾米壁纸微信小步伐下载图片到相册saveImageToPhotosAlbum功能修改 ...

打印 上一主题 下一主题

主题 888|帖子 888|积分 2664

当我将咸虾米壁纸这个项目进行重构的时候,想要将图片资源变得小一点,所以在上传图片的时候均采用了webp格式的,这样就导致再预览页面下载图片的时候堕落了,之前使用的是uni.getImageInfo()这个API,该API不支持webp图片格式获取暂时地址,所以这篇文章将getImageInfo方法改成uni.downloadFile()方法便可解决。

  

原来的方法

原文链接:https://blog.csdn.net/qq_18798149/article/details/135871140
  1. //点击下载
  2. const clickDownload = async () => {
  3.         try {
  4.                 uni.showLoading({
  5.                         title: "下载中...",
  6.                         mask: true
  7.                 })
  8.                 let res = await downPushData();
  9.                 if(res.errCode != 0) throw res;                       
  10.                 // #ifdef MP || APP                               
  11.                 uni.getImageInfo({
  12.                         src: crtWallInfo.value.picurl,
  13.                         success: function(res) {                                       
  14.                                 var path = res.path;
  15.                                 uni.saveImageToPhotosAlbum({
  16.                                         filePath: path,
  17.                                         success(res) {
  18.                                                 uni.hideLoading();
  19.                                                 uni.showToast({
  20.                                                         title: '保存成功,可去相册查看',
  21.                                                         icon: "none",
  22.                                                         duration:2000
  23.                                                 })                                                       
  24.                                         },
  25.                                         fail(err) {
  26.                                                 uni.hideLoading();
  27.                                                 if(err.errMsg == 'saveImageToPhotosAlbum:fail cancel'){
  28.                                                         uni.showToast({
  29.                                                                 title: '保存失败,请重新点击下载',
  30.                                                                 icon: "none"
  31.                                                         })
  32.                                                         return;
  33.                                                 }                                                       
  34.                                                 uni.showModal({
  35.                                                         title: '提示',
  36.                                                         content: '需要您授权保存相册',
  37.                                                         showCancel: false,
  38.                                                         success:res=>{
  39.                                                                 if(res.confirm){
  40.                                                                         uni.openSetting({
  41.                                                                                 success(settingdata) {
  42.                                                                                         if (settingdata.authSetting['scope.writePhotosAlbum']) {
  43.                                                                                                 uni.showToast({
  44.                                                                                                         title:'获取权限成功',
  45.                                                                                                         icon:"none"
  46.                                                                                                 })                                                                                                       
  47.                                                                                         }else{
  48.                                                                                                 uni.showToast({
  49.                                                                                                         title:'获取权限失败',
  50.                                                                                                         icon:"none"
  51.                                                                                                 })                                                                                                       
  52.                                                                                         }
  53.                                                                                        
  54.                                                                                 }
  55.                                                                         })
  56.                                                                 }
  57.                                                         }
  58.                                                 })
  59.                                         },
  60.                                         complete(err) {
  61.                                                
  62.                                         }
  63.                                 })
  64.                         }
  65.                 })
  66.                 // #endif
  67.                 // #ifdef H5
  68.                 //调用预览图片的方法               
  69.                 uni.previewImage({
  70.                         urls: [crtWallInfo.value.picurl],
  71.                         current: 0, //点击图片传过来的下标
  72.                         success: (res) => {
  73.                                 uni.showToast({
  74.                                         title: '请长按保存',
  75.                                         icon: "none",
  76.                                         duration: 2000
  77.                                 })
  78.                         }
  79.                 })
  80.                 // #endif
  81.         } catch (err) {                       
  82.                 console.log(err);
  83.                 uni.hideLoading();
  84.         }
  85. }
复制代码
上面这个方法,通过uni.getImageInfo()无法获取webp格式,所以进行改造,使用uni.downloadFile()这个API,不外这个API返回的参数和上面有差别,注意对比。
修改后代码

  1. //点击下载
  2. const clickDownload = async () => {
  3.         if(!gotoLogin()) return;
  4.         let {_id,classid} = currentInfo.value;               
  5.        
  6.         // #ifdef H5
  7.         let feedback = await showModal({content:"请长按保存壁纸"})
  8.         if(feedback == 'confirm') actionCloudObj.writeDownload({picid:_id,classid})
  9.         // #endif
  10.         // #ifndef H5
  11.         try {
  12.                 uni.showLoading({
  13.                         title: "下载中...",
  14.                         mask: true
  15.                 })                       
  16.                 uni.downloadFile({
  17.                         url: currentInfo.value.picurl,
  18.                         success: (res) => {
  19.                                 console.log(res);
  20.                                 uni.saveImageToPhotosAlbum({
  21.                                         filePath: res.tempFilePath,
  22.                                         success: (res) => {
  23.                                                 uni.showToast({
  24.                                                         title: "保存成功,请到相册查看",
  25.                                                         icon: "none"
  26.                                                 })
  27.                                                 actionCloudObj.writeDownload({picid:_id,classid})
  28.                                         },
  29.                                         fail: err => {
  30.                                                 if (err.errMsg == 'saveImageToPhotosAlbum:fail cancel') {
  31.                                                         uni.showToast({
  32.                                                                 title: '保存失败,请重新点击下载',
  33.                                                                 icon: "none"
  34.                                                         })
  35.                                                         return;
  36.                                                 }
  37.                                                 uni.showModal({
  38.                                                         title: "授权提示",
  39.                                                         content: "需要授权保存相册",
  40.                                                         success: res => {
  41.                                                                 if (res.confirm) {
  42.                                                                         uni.openSetting({
  43.                                                                                 success: (setting) => {
  44.                                                                                         console.log(
  45.                                                                                                 setting);
  46.                                                                                         if (setting
  47.                                                                                                 .authSetting[
  48.                                                                                                         'scope.writePhotosAlbum'
  49.                                                                                                         ]) {
  50.                                                                                                 uni.showToast({
  51.                                                                                                         title: "获取授权成功",
  52.                                                                                                         icon: "none"
  53.                                                                                                 })
  54.                                                                                         } else {
  55.                                                                                                 uni.showToast({
  56.                                                                                                         title: "获取权限失败",
  57.                                                                                                         icon: "none"
  58.                                                                                                 })
  59.                                                                                         }
  60.                                                                                 }
  61.                                                                         })
  62.                                                                 }
  63.                                                         }
  64.                                                 })
  65.                                         },
  66.                                         complete: () => {
  67.                                                 uni.hideLoading();
  68.                                         }
  69.                                 })
  70.                         },
  71.                         fail(err) {
  72.                                 console.log(err);
  73.                         }
  74.                 })
  75.         } catch (err) {
  76.                 console.log(err);
  77.                 uni.hideLoading();
  78.         }
  79.         // #endif
  80. }
复制代码
该方法同时也给uni.downloadFile增加了fail错误的实行方法,在之前代码中忘了加这个错误的返回值了,所以找了半天原因才知道uni.getImageInfo不支持webp格式。
注意:
uni.downloadFile()该API通报的参数是url,这个是你服务器返回的地址,success返回的暂时地址,放在tempFilePath属性内,使用uni.saveImageToPhotosAlbum()存入相册时,注意改一下filePath=res.tempFilePath。
假如文章帮助到你,请记得给点赞支持一下哦。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

缠丝猫

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表