成都創(chuàng)新互聯(lián)專注于沙河企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè)公司,商城網(wǎng)站開發(fā)。沙河網(wǎng)站建設(shè)公司,為沙河等地區(qū)提供建站服務(wù)。全流程按需定制設(shè)計,專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
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方法會返回一個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) //計算讓畫布圖片自適應(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 請求中其他額外的 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è)置高度寬度就可以 圖片會鋪滿屏幕
總結(jié)
以上所述是小編給大家介紹的微信小程序點擊保存圖片到本機(jī)功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對創(chuàng)新互聯(lián)網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉(zhuǎn)載,煩請注明出處,謝謝!
分享題目:微信小程序點擊保存圖片到本機(jī)功能
網(wǎng)頁URL:http://www.rwnh.cn/article42/jischc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊、外貿(mào)網(wǎng)站建設(shè)、營銷型網(wǎng)站建設(shè)、標(biāo)簽優(yōu)化、ChatGPT、網(wǎng)站設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)