执子左
执子左
  • 发布:2019-11-11 11:28
  • 更新:2022-02-15 19:05
  • 阅读:17371

uni-app开发微信小程序如何将页面某一View内容生成图片并保存在本地

分类:uni-app
2019-11-11 11:28 负责人:无 分享
已邀请:
LukeDev2K

LukeDev2K - 小程序,app

小程序截图一直没有看到好的方案

龙雨溪

龙雨溪

view的不能,你得用canvas

  • 执子左 (作者)

    怎么操作呀?我不是很会前端,写后台的,临时任务,就差最后一步了

    2019-11-11 11:31

  • 龙雨溪

    回复 执子左: 哈哈,你这一步可不小,看下canvas文档

    2019-11-11 11:40

  • 执子左 (作者)

    回复 龙雨溪: 0.0好吧,谢了

    2019-11-11 12:26

执子左

执子左 (作者) - 一个菜鸟。。。

搞定!

  • 梅花三

    和你同样的问题 请问你是怎么解决的

    2019-12-25 16:00

  • 序到自然

    怎么写的啊

    2021-05-07 12:00

  • 9***@qq.com

    请问怎么结局的

    2023-10-13 14:59

3***@qq.com

3***@qq.com

请问你这个问题是怎么解决的?

  • 执子左 (作者)

    <canvas canvas-id="playbillCanvas"></canvas>

    drawCanvas() {

    uni.showLoading({

    title: "loading"

    });

    var _this = this;

    var context = uni.createCanvasContext('playbillCanvas');

    context.drawImage(_this.areaImage, 0, 0, uni.upx2px(550), uni.upx2px(750));

    context.draw(true);


                base64ToPath(_this.qr).then(path => {  
    context.save();
    context.drawImage(path, _this.oldImage.imageX, _this.oldImage.imageY, uni.upx2px(_this.image_size), uni.upx2px(
    _this.image_size));
    context.restore();
    context.draw(true);
    }).catch(error => {
    console.error(error)
    })

    context.save();
    context.setFontSize(uni.upx2px(_this.textFontSize));
    context.setFillStyle(_this.textColor);
    context.fillText(_this.message, _this.oldText.textX, _this.oldText.textY+10);
    context.restore();
    context.draw(true);

    setTimeout(function() {
    uni.canvasToTempFilePath({
    canvasId: 'playbillCanvas',
    success: function(res) {
    uni.hideLoading();
    const imageList = [];
    imageList.push(res.tempFilePath);
    uni.previewImage({
    urls: imageList,
    indicator: "default",
    loop: false
    });
    },
    fail: function() {
    uni.hideLoading();
    uni.showToast({
    title: "生成海报失败!"
    });
    }
    })
    }, 2000);
    },

    2020-02-12 10:56

  • 执子左 (作者)

    仅供参考,很久不做了,有些忘了,重点就是使用canvas。官方插件市场有案例

    2020-02-12 10:57

  • 3***@qq.com

    回复 执子左: 好的。谢谢您

    2020-02-12 11:01

  • ZHT

    回复 执子左: _this.areaImage 是 View 的画面?这个是怎么获取到的

    2020-03-18 16:57

  • cybccc

    回复 执子左: 能把全部代码拿出来看看吗

    2021-03-18 10:34

1***@qq.com

1***@qq.com - ...

能实现吗?把某一块的内容生成照片

3***@qq.com

3***@qq.com

好假 搞定这个词 好假 我想不通 怎么把view 里面的内容弄成一个 arealimgae 你是怎么办到的

1***@163.com

1***@163.com

我可以生成图片并保存手机,但是我还想给他分享出去,在想办法把一块view生成图片并分享出去,主要看保存方法,我删减其它东西了。有分享微信好友朋友圈和保存本地相册 <template> <view> <view class="friend-conten"> <view style="margin-top: 80rpx;" class="renderjs" id="poster"> <view class="friend-image"> <image src="/static/images/image-noLogin/img-log-friend-bg.png"></image> </view> <view class="friend-box"> <view class="friend-box-bottom"> <view class="friend-boxrecomm"> <view class="friend-box-top"> <image mode="aspectFill" :src="myUrl"></image> <view class="friend-box-text">{{myName}}</view> </view> <view class="friend-box-con">我正在使用花卉APP买卖鲜花</view> <view class="friend-box-boot-text">长按识别二维码 立即下载</view> </view> <view class="friend-box-qrcode"> <tki-qrcode :lv="2" :width="134" :height="134" ref="qrcode" :val="qrCodePath" :size="134" loadMake="true" :showLoading="false" @result="generateCodeResult" /> </view>
</view>
</view>
</view>
</view>
<view class="friend-bnt" @click="clickFriend()">
推荐好友
</view>
<view class="body-friend" v-show="frienType==true">
<view class="body-friend-box">
<view class="body-friend-text">推荐「花卉」给你好友吧</view>
<view class="body-friend-image-box">
<view class="body-friend-image" @click="renderScript.emitData">
<image src="/static/images/public/icon-preservation.png"></image>
保存图片
</view>
<view class="body-friend-image" @click="renderScript.emitDatai">
<image src="/static/images/public/img-share-friend.png"></image>
分享微信
</view>
<view class="body-friend-image" @click="wechatMoments()">
<image src="/static/images/public/icon-share.png"></image>
分享朋友圈
</view>
</view>
<view class="body-friend-cancel" @click="clickFriend()">取消</view>
</view>
</view>
</view>
</template>

<script>
import tkiQrcode from "@/components/tki-qrcode/tki-qrcode.vue";
import html2canvas from "html2canvas";
export default {
components: {
tkiQrcode,
html2canvas
},
data() {
return {
qrCodePath: "12312",
myName: "",
myUrl: "",
frienType: false,
//保存图片
base64: "",

        };  
    },  
    onLoad() {  
        //获取本地个人信息  
        let seleData = uni.getStorageSync("userInfo");  
        if (seleData !== "" && seleData != null) {  
            this.myUrl = seleData.avatar;  
            this.myName = seleData.nickName;  
        }  
    },  
    methods: {  

        // 分享朋友圈  
        wechatMoments() {  
            uni.share({  
                provider: "weixin", //分享到weixin 、 QQ 、sinaweibo  
                scene: "WXSenceTimeline", //WXSceneSession聊天界面、WXSenceTimeline朋友圈、WXSceneFavorite微信收藏  
                type: 0, //分享形式,如图文、纯文字、纯图片、音乐、视频、小程序等。默认图文 0。不同分享服务商支持的形式不同,具体参考下面type值说明。  
                href: "http://uniapp.dcloud.io/",  
                title: "uni-app分享", //分享标题  
                summary: "快来和我一起",  
                imageUrl: "https://img1.baidu.com/it/u=1461484221,2134526266&fm=26&fmt=auto&gp=0.jpg", //分享显示图片  
                // mediaUrl:""//音频地址  
                success: function(res) { //成功执行  
                    console.log("success:" + JSON.stringify(res));  
                    uni.showToast({  
                        title: "分享成功",  
                        icon: "none",  
                    })  
                },  
                fail: function(err) { //失败执行  
                    console.log("fail:" + JSON.stringify(err));  
                    uni.showToast({  
                        title: "分享失败",  
                        icon: "none",  
                    })  
                },  
                complete: function() {  
                    // 成功失败都执行  
                    console.log("--------------")  
                }  
            });  
        },  
        //分享微信好友  
        wechatFriends() {  
            console.log(this.base64);  
            // let base64 = uni.getStorageSync("base");  
            uni.share({  
                provider: "weixin", //分享到weixin 、 QQ 、sinaweibo  
                scene: "WXSceneSession", //WXSceneSession聊天界面、WXSenceTimeline朋友圈、WXSceneFavorite微信收藏  
                type: 2, //分享形式,如图文、纯文字、纯图片、音乐、视频、小程序等。默认图文 0。不同分享服务商支持的形式不同,具体参考下面type值说明。  
                imageUrl: imgFile, //分享显示图片  
                // mediaUrl:""//音频地址  
                success: function(res) { //成功执行  
                    console.log("success:" + JSON.stringify(res));  
                    uni.showToast({  
                        title: "分享成功",  
                        icon: "none",  
                    })  
                },  
                fail: function(err) { //失败执行  
                    console.log("fail:" + JSON.stringify(err), "分享失败");  
                    uni.showToast({  
                        title: "分享失败",  
                        icon: "none",  
                    })  
                },  
                complete: function() {  
                    // 成功失败都执行  
                    console.log("-----成功失败都执行---------")  
                }  
            });  

        },  
        // 接收renderjs发回的数据  
        receiveRenderData(val) {  
            this.base64 = val  
            if (this.base64 != null) {  
                let base64 = this.base64;  
                const bitmap = new plus.nativeObj.Bitmap("test");  
                bitmap.loadBase64Data(base64, function() {  
                    const url = "_doc/" + new Date().getTime() + ".png"; // url为时间戳命名方式  
                    console.log('saveHeadImgFile', url)  
                    bitmap.save(url, {  
                        overwrite: true, // 是否覆盖  
                        // quality: 'quality'  // 图片清晰度  
                    }, (i) => {  
                        uni.saveImageToPhotosAlbum({  
                            filePath: url,  
                            success: function() {  
                                uni.showToast({  
                                    title: '图片保存成功',  
                                    icon: 'none'  
                                })  
                                console.log(url, "url");  
                                bitmap.clear()  
                            }  
                        });  
                    }, (e) => {  
                        uni.showToast({  
                            title: '图片保存失败',  
                            icon: 'none'  
                        })  
                        bitmap.clear()  
                    });  
                }, (e) => {  
                    uni.showToast({  
                        title: '图片保存失败',  
                        icon: 'none'  
                    })  
                    bitmap.clear()  
                });  
            }  
        },  
        // 接收renderjs发回的数据  
        receiveRenderDatai(val) {  
            this.base64 = val;  
            if (this.base64 !== undefined || this.base64 !== "") {  
                this.wechatFriends();  
            }  

        },  
    },  
    // 监听页面返回事件(侧滑返回、点击返回按钮)  
    onBackPress(options) {  
        if (this.frienType) {  
            this.frienType = false;  
            return true;  
        }  
    }  
}  

</script>
<script module="renderScript" lang="renderjs">
import html2canvas from 'html2canvas';
export default {
data() {
return {

        }  
    },  
    methods: {  
        // 发送数据到逻辑层  
        emitData(e, ownerVm) {  
            const dom = document.getElementById('poster')  
            html2canvas(dom, {  
                width: dom.clientWidth, //dom 原始宽度  
                height: dom.clientHeight,  
                scrollY: 0, // html2canvas默认绘制视图内的页面,需要把scrollY,scrollX设置为0  
                scrollX: 0,  
                useCORS: true, //支持跨域,但好像没什么用  
            }).then((canvas) => {  
                // 将生产的canvas转为base64图片3  
                ownerVm.callMethod('receiveRenderData', canvas.toDataURL('image/png'));  
            });  
        },  

    }  
};  

</script>
<style lang="scss">

</style>

  • 1***@qq.com

    我虽然看不懂(指格式) 但我大为震撼

    2021-11-04 11:54

2***@qq.com

2***@qq.com

怎么解决的

纵行风云

纵行风云 - 测试

大佬可以看一下dome代码吗?

该问题目前已经被锁定, 无法添加新回复