微信小程序點(diǎn)擊保存圖片到本機(jī)功能
1.首先我們要把想保存的圖片繪制在畫布上
<view class='container'> <canvas style='width:{{canvasWidth}}px; height:{{canvasHeight}}px' class="canvas" id="canvas" canvas-id="canvas" disable-scroll="true"> </canvas> <button bindtap='clickMe'>保存圖片</button> </view>
2.我們在看看看js代碼在用wx.canvasToTempFilePath方法會(huì)返回一個(gè)tempFilePath圖片路徑
// canvas 全局配置 var context = null; var rpx var posterHeight = 0 var posterWidth = 0 var avatarPadding = 0 //距離邊界 var avatarRadiu = 0 //頭像半徑 var textScale = 0 //文字比例 //注冊頁面 Page({ data: { img: "../../images/img1.jpg", myCanvasWidth: 0, myCanvasHeight: 0, posterHeight: 0, }, onLoad: function (options) { var that = this var myCanvasWidth = that.data.myCanvasWidth //為了讓圖片滿鋪頁面 var myCanvasHeight = that.data.canvasHeight context = wx.createCanvasContext('canvas'); wx.getSystemInfo({ success: function (res) { myCanvasWidth = res.screenWidth myCanvasHeight = res.screenHeight posterWidth = Math.round(res.screenWidth * 0.68) //計(jì)算讓畫布圖片自適應(yīng) posterHeight = Math.round(posterWidth * 1920 / 1080) avatarPadding = Math.round(posterWidth * 20 / 375) avatarRadiu = Math.round(posterWidth * 25 / 375) textScale = Math.round(posterWidth / 375) rpx = res.windowWidth / 375; that.setData({ myCanvasWidth: myCanvasWidth, myCanvasHeight: myCanvasHeight, posterHeight: posterHeight }) context.drawImage(that.data.img, 0, 0, that.data.myCanvasWidth, that.data.myCanvasHeight); //畫布繪制圖片 context.draw(); }, }) }, clickMe: function () { //保存圖片 wx.canvasToTempFilePath({ canvasId: 'canvas', fileType: 'jpg', success: function (res) { console.log(res) wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success(res) { console.log(res) wx.hideLoading(); wx.showToast({ title: '保存成功', }); // //存入服務(wù)器 // wx.uploadFile({ // url: 'a.php', //接口地址 // filePath: res.tempFilePath, // name: 'file', // formData: { //HTTP 請(qǐng)求中其他額外的 form data // 'user': 'test' // }, // success: function (res) { // console.log(res); // }, // fail: function (res) { // console.log(res); // }, // complete: function (res) { // } // }); }, fail() { wx.hideLoading() } }) } }) }, })
3,css樣式 直接給畫布設(shè)置高度寬度就可以 圖片會(huì)鋪滿屏幕
總結(jié)
以上所述是小編給大家介紹的微信小程序點(diǎn)擊保存圖片到本機(jī)功能,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
js與jquery獲取input輸入框中的值實(shí)例講解
在本篇文章里小編給大家整理了關(guān)于js與jquery獲取input輸入框中的值實(shí)例講解,需要的朋友們可以學(xué)習(xí)參考下。2020-02-02使用原生javascript創(chuàng)建通用表單驗(yàn)證——更鋒利的使用dom對(duì)象
使用原生javascript創(chuàng)建通用表單驗(yàn)證——更鋒利的使用dom對(duì)象,學(xué)習(xí)js的朋友可以參考下。2011-09-09JavaScript設(shè)計(jì)模式之策略模式實(shí)例
這篇文章主要介紹了JavaScript設(shè)計(jì)模式之策略模式實(shí)例,本文分析了Jquery源碼并給出了自己的實(shí)現(xiàn),需要的朋友可以參考下2014-10-10JS實(shí)現(xiàn)指定區(qū)域的全屏顯示功能示例
這篇文章主要介紹了JS實(shí)現(xiàn)指定區(qū)域的全屏顯示功能,涉及javascript事件響應(yīng)及頁面元素屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04jsonp實(shí)現(xiàn)百度下拉框功能的方法分析
這篇文章主要介紹了jsonp實(shí)現(xiàn)百度下拉框功能的方法,結(jié)合實(shí)例形式分析了調(diào)用百度接口jsonp實(shí)現(xiàn)跨域請(qǐng)求及內(nèi)容渲染相關(guān)操作技巧,需要的朋友可以參考下2019-05-05