<template> <view> <canvas :canvas-id="canvasId" class="my-canvas" :style="{ width: canvasWidth+'px', height: canvasHeight+'px' }"></canvas> </view> </template> <script> export default { data() { return { ctx: null, canvasId: 'canvas_id', canvasWidth: 0, canvasHeight: 0 }; }, created() { this.ctx = uni.createCanvasContext(this.canvasId, this); }, methods: { // 旋转图片 rotateImage(src, angle) { uni.getImageInfo({ src }).then(res => { const { width, height } = res this.canvasWidth = height this.canvasHeight = width setTimeout(() => { this.ctx.clearRect(0, 0, width, height) this.ctx.rotate(angle * Math.PI / 180) this.ctx.translate(0, -height) this.ctx.drawImage(src, 0, 0, width, height) this.ctx.draw() uni.canvasToTempFilePath({ canvasId: this.canvasId, quality: 1, success: res => { this.$emit('success', res.tempFilePath) }, fail: err => { console.error(err) this.$emit('fail', err) } }, this) }, 200) }) } } } </script> <style scoped> .my-canvas { overflow: hidden !important; position: fixed !important; z-index: -100000 !important; visibility: hidden !important; top: 400% !important; left: 400% !important; } </style>
小程序使用canvas旋转图片,内存不足的问题?业务需求,有一张图片本地图片,需要进行旋转生成新的图片 使用canvas实现,第1次点击正常,第2次点击会提示内存不足,或者直接闪退 调试基础库3.1.0 ios真机微信版本8.0.41 操作视频可点击链接查看: https://pan.baidu.com/s/15F1HFzpfSZxPlX1vL4DuGw?pwd=1234 手机微信日志11:00-12:00日志已上传,崩溃触发时间是11:23,微信号是lifei8241102
2023-09-20