vue打包app中,借助HTML5+API下載圖片問題

1、線上圖片地址(http、https):


源碼:

downLoad() {

? ? ? ? plus.nativeUI.showWaiting('loading...');

? ? ? ? var that = this;

? ? ? ? ? ? var dtask = plus.downloader.createDownload('imgUrl', {}, function (d, status) {

? ? ? ? ? ? ? // 下載完成

? ? ? ? ? ? ? if (status == 200) {

? ? ? ? ? ? ? ? plus.gallery.save(d.filename, function () {//保存到相冊(cè)方法

? ? ? ? ? ? ? ? ? plus.nativeUI.closeWaiting()

? ? ? ? ? ? ? ? ? plus.nativeUI.toast('<div>已保存到手機(jī)相冊(cè)</div>',{

? ? ? ? ? ? ? ? ? ? type:'richtext'

? ? ? ? ? ? });

? ? ? ? ? ? ? ? }, function () {

? ? ? ? ? ? ? ? ? plus.nativeUI.closeWaiting()

? ? ? ? ? ? ? ? ? plus.nativeUI.toast('<div>下載失敗,請(qǐng)重試</div>',{

? ? ? ? ? ? ? ? ? ? type:'richtext'

? ? ? ? ? ? });

? ? ? ? ? ? ? ? });

? ? ? ? ? ? ? } else {

? ? ? ? ? ? ? plus.nativeUI.closeWaiting()

? ? ? ? ? ? ? ? plus.nativeUI.toast('<div>下載失敗,請(qǐng)重試</div>',{

? ? ? ? ? ? ? ? ? ? type:'richtext'

? ? ? ? ? ? });

? ? ? ? ? ? ? }

? ? ? ? ? ? });

? ? ? ? ? ? dtask.start();

? ? }

2、canvas合成base64圖片下載

轉(zhuǎn)載:http://www.itdecent.cn/p/c336fa712da9


源碼:

downLoad (base64,?success,?error)?{

? plus.nativeUI.showWaiting('loading...');

??var?bitmap?=?new?window.plus.nativeObj.Bitmap('image');? // 創(chuàng)建Bitmap對(duì)象

? var that = this;

? // 調(diào)用load/loadBase64Data方法加載圖片

??bitmap.loadBase64Data(base64,?function?(i)?{

that.bigmapToSave(bitmap,?success,?error)

??},?function?(err)?{

plus.nativeUI.closeWaiting()

????window.plus.nativeUI(err.message)

??})

},

// 調(diào)用保存bitmap的圖片到本機(jī)系統(tǒng)文件,為之后相冊(cè)保存提供路徑

bigmapToSave?(bitmap,?success,?error)?{

??var?name?=?this.randomString(10)

? var that = this;

??bitmap.save(`_doc/${name}.jpg`,?{?overwrite:?false?},?function?(event)?{

????console.log(event.target)

that.imageSaveByGallery(event.target,?success,?error)

??},?function?(err)?{

plus.nativeUI.closeWaiting()

????window.plus.nativeUI(err.message)

??})

},

// 圖片保存

imageSaveByGallery?(url,?success,?error)?{

??window.plus.gallery.save(url,?function?(event)?{

????const?tarbitmap?=?window.plus.nativeObj.Bitmap.getBitmapById('image')

????tarbitmap.clear()

plus.nativeUI.closeWaiting()

????plus.nativeUI.toast('<div>已保存到手機(jī)相冊(cè)</div>',{

? ? ? ? ? ? ? ? ? ? type:'richtext'

? ? ? ? });

??},?function?(err)?{

plus.nativeUI.closeWaiting()

????plus.nativeUI.toast('<div>下載失敗,請(qǐng)重試</div>',{

? ? ? ? ? ? ? ? ? ? type:'richtext'

? ? ? ? ? ? });

??})

},

randomString (e)?{

??var d?=?e?||?32

??var?t?=?'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678'

??var?a?=?t.length

??var?n?=?''

??for?(var?i?=?0;?i?<?d;?i++)?n?+=?t.charAt(Math.floor(Math.random()?*?a))

??return?n

},

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容