uniapp分享圖片(canvas標簽內容生成圖片並保存)


APP:

  1.異步加載圖片數組內容
 1     // 1.異步加載圖片數組內容
 2     promiseImgArray(api) {
 3         return (options, ...params) => {
 4             return new Promise((resolve, reject) => {
 5                 const extras = {
 6                     success: resolve,
 7                     fail: reject
 8                 }
 9                 api({
10                     ...options,
11                     ...extras
12                 }, ...params)
13             })
14         }
15     },

  2.繪制准備分享的圖片內容

 1     // 2.繪制准備分享的圖片內容 繪制單位為px
 2     canvasSaveImage(shareInfo) {
 3     let getImgInfo = this.promiseImgArray(uni.getImageInfo);
 4     Promise.all([getImgInfo({
 5                 src: '../../static/images/share-bg-img.png'
 6             }), getImgInfo({
 7                 src: this.$baseURL + this.shareImg
 8             }), getImgInfo({
 9                 src: '../../static/images/icon-time.png'
10             }) ])
11     .then(res => {
12         console.log(res)
13         // 獲取頁面上的canvas標簽的canvas-id
14         const ctx = uni.createCanvasContext('shareCanvas');
15         // 繪制圖片(背景圖要首先繪制,不然會被遮蓋)
16         ctx.drawImage(res[0].path, 0, 0, 325, 391);
17 
18         ctx.setFillStyle('white')
19         ctx.setFontSize(16)
20         // 固定文本內容
21         ctx.fillText("有點小九九", 8, 62);
22 
23         ctx.setFillStyle('#777777')
24         ctx.setFontSize(13)
25         // 變量內容
26         ctx.fillText(shareInfo.create_time, 46, 137);
27 
28         ctx.setFillStyle('#333333')
29         ctx.setFontSize(14)
30         // 設置文本樣式
31         ctx.font = "bold 14px sans-serif"
32         // 文本換行 (第3個方法)
33         this.drawText(ctx, '【' + shareInfo.title + '】', 20, 163, 49, 285)
34         // 划線
35         ctx.beginPath()
36         ctx.moveTo(20, 293)
37         ctx.lineTo(305, 293)
38 
39         ctx.setFillStyle('#333333')
40         ctx.setFontSize(12)
41         // 可以添加國際化內容
42         ctx.fillText(this.$t('information.shareText2'), 88, 321);
43 
44         const qrImgSize = 60
45         // 繪制圖片
46         ctx.drawImage(res[1].path, 20, 309, qrImgSize, qrImgSize)
47         ctx.stroke();
48         // 繪制整圖
49         ctx.draw(false, () => {
50             // 把canvas生成為圖片
51             this.tempFileImage()
52         })
53     })
54     .catch(err=>{
55         console.log(err)
56     })
57     },
 
        
  3.控制繪制文本換行
 1     // 3.控制繪制文本換行
 2     drawText: function(ctx, str, leftWidth, initHeight, titleHeight, canvasWidth) {
 3     let lineWidth = 0;
 4     let lastSubStrIndex = 0; //每次開始截取的字符串的索引
 5     for (let i = 0; i < str.length; i++) {
 6         lineWidth += ctx.measureText(str[i]).width;
 7         if (lineWidth > canvasWidth) {
 8             ctx.fillText(str.substring(lastSubStrIndex, i), leftWidth, initHeight); //繪制截取部分
 9             initHeight += 22; //22為 文字大小20 + 2
10             lineWidth = 0;
11             lastSubStrIndex = i;
12             titleHeight += 22;
13         }
14         if (i == str.length - 1) { //繪制剩余部分
15             ctx.fillText(str.substring(lastSubStrIndex, i + 1), leftWidth, initHeight);
16         }
17     }
18     // 標題border-bottom 線距頂部距離
19     titleHeight = titleHeight + 10;
20     return titleHeight;
21     },
 
        

   4.把當前畫布指定區域的內容導出生成指定大小的圖片

 
        
 1     //  4.把當前畫布指定區域的內容導出生成指定大小的圖片
 2     tempFileImage() {
 3     let that = this
 4     uni.canvasToTempFilePath({
 5         canvasId: 'shareCanvas',
 6         success: (res) => {
 7             console.log(res, 'tempFileImage')
 8             uni.hideLoading()
 9             // 保存當前繪制圖片
10             that.savePic(res.tempFilePath)
11         },
12         fail: function(err) {
13             console.log(err, '圖片生成失敗')
14         }
15     })
16 
17     }, 

   5.保存圖片到本地

 1     // 5.保存圖片到本地
 2     savePic(filePath) {
 3     // #ifdef APP-PLUS
 4     uni.showLoading({
 5         title: '正在保存'
 6     });
 7     uni.saveImageToPhotosAlbum({
 8         filePath: filePath,
 9         success: function() {
10             uni.showToast({
11                 title: '圖片保存成功~'
12             });
13         },
14         fail: function(e) {
15             console.log(e, '圖片保存失敗')
16         },
17         complete: function() {
18             uni.hideLoading()
19         }
20     });
21     // #endif
22     // #ifdef H5
23     // uni.saveImageToPhotosAlbum 並不兼容h5 寫個兼容h5的方法 不一定有用
24     var oA = document.createElement('a');
25     oA.download = 'share';
26     oA.href = data.tempFilePath;
27     document.body.appendChild(oA);
28     oA.click();
29     oA.remove(); // 下載之后把創建的元素刪除
30     // #endif
31     },

  完工

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM