日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

uniapp保存图片_Uniapp实现保存图片到相册(封装起来)

發布時間:2025/1/21 编程问答 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 uniapp保存图片_Uniapp实现保存图片到相册(封装起来) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

我封裝了一個方法,之后可以直接使用

在此之前,先用class封裝一個Toast提示框

class Toast{

constructor(title,icon="success",duration=1300,mask=true) {

this.title = title

this.icon = icon

this.duration = duration

this.mask = mask

}

// 消息提示框:自動消失

showtoast(){

uni.showToast({

title: this.title,

icon:this.icon,

duration: this.duration,

mask:this.mask

});

}

// 消息提示框,手動消失

showloading(){

uni.showLoading({

title: this.title,

mask:true

});

}

// 輕提示

}

export default Toast

然后就開始封裝我們的downloadImg.js了

import toast from './toast.js'

export default function downloadImg(url){

uni.showLoading({

title: '正在下載',

mask: true

})

console.log(url)

let that = this;

uni.getImageInfo({

src: url,

success(res) {

console.log(res.path)

uni.saveImageToPhotosAlbum({

filePath: res.path,

success(res) {

new toast("已保存至相冊").showtoast()

},

fail(err) {

console.log(err);

uni.hideLoading();

uni.showModal({

title: '保存圖片至相冊',

content: '需要獲取您的相冊權限,請確認授權',

success: function(res) {

if (res.cancel) {

uni.showToast({

title: '獲取相冊權限失敗',

icon: 'none',

duration: 2000

})

} else if (res.confirm) {

uni.showLoading({

title: '打開設置',

mask: true

})

uni.openSetting({

success: function(data) {

uni.hideLoading();

new toast("請重新點擊下載原圖").showloading()

}

})

}

}

})

}

})

},

fail(err) {

console.log(err);

new toast("下載失敗,請重試").showloading()

}

})

}

然后最好把它掛載到原型上面,這樣就能全局使用了

import previewLogic from './public/previewImg.js'

Vue.prototype.$previewLogic = previewLogic

之后項目中如何使用呢?

previewImg(index,imgArray){

new this.$previewLogic(index, imgArray).previewImg()

}

index就是圖片的下標

imgArray是包含圖片的數組。例如:[‘圖片1’,‘圖片2’…]

previewImg,index,imgArray是自定義的,不是標準

總結

以上是生活随笔為你收集整理的uniapp保存图片_Uniapp实现保存图片到相册(封装起来)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。