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 () {//保存到相册方法
plus.nativeUI.closeWaiting()
plus.nativeUI.toast('<div>已保存到手机相册</div>',{
type:'richtext'
});
}, function () {
plus.nativeUI.closeWaiting()
plus.nativeUI.toast('<div>下载失败,请重试</div>',{
type:'richtext'
});
});
} else {
plus.nativeUI.closeWaiting()
plus.nativeUI.toast('<div>下载失败,请重试</div>',{
type:'richtext'
});
}
});
dtask.start();
}
2、canvas合成base64图片下载
转载:https://www.jianshu.com/p/c336fa712da9
源码:
downLoad (base64, success, error) {
plus.nativeUI.showWaiting('loading...');
var bitmap = new window.plus.nativeObj.Bitmap('image'); // 创建Bitmap对象
var that = this;
// 调用load/loadBase64Data方法加载图片
bitmap.loadBase64Data(base64, function (i) {
that.bigmapToSave(bitmap, success, error)
}, function (err) {
plus.nativeUI.closeWaiting()
window.plus.nativeUI(err.message)
})
},
// 调用保存bitmap的图片到本机系统文件,为之后相册保存提供路径
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>已保存到手机相册</div>',{
type:'richtext'
});
}, function (err) {
plus.nativeUI.closeWaiting()
plus.nativeUI.toast('<div>下载失败,请重试</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
},