uniapp 将图片转成JPG格式

<template>

<view style="width: 100%; height: 100%;">

<image :src="imagePath" lazy-load style="width: 100%; height: 100%;"></image>

<canvas

style="background-color: red;"

:style="{width: target.width + 'px', height: target.height + 'px'}" canvas-id="target"></canvas>

</view>

</template>

<script>

export default {

data() {

return {

targetContext: null,

imagePath: '',

target: {

width: 300,

height: 300

}

}

},

onLoad(e) {

console.log(e);

// this.imagePath = e.imagepath;

this.targetContext = uni.createCanvasContext("target", this); //第一个画布

this.imageDetailMsg(e.imagepath);

},

methods: {

imageDetailMsg(imageUrl) {

let self = this;

uni.getImageInfo({

src: imageUrl,

success: function(imageInfo) {

console.log(imageInfo);

setTimeout(() => {

self.targetContext.drawImage(imageInfo.path, 0, 0, 300, 300, 0, 0, 300, 300);

self.targetContext.draw(false, () => {

uni.canvasToTempFilePath({

canvasId: "target",

fileType:'jpg',

success: (res) => {

var path = res.tempFilePath;

console.log('裁剪后的图片',res);

self.imagePath = path;

},

fail: (ev) => {

console.log(ev);

},

complete: () => {

}

}, self);

});

}, 100);

}

})

},

}

}

</script>

<style>

</style>

相关推荐
whinc6 小时前
JavaScript技术周刊 2026年第18周
javascript
码海扬帆:前端探索之旅6 小时前
深度定制 uni-combox:新增功能详解与实战指南
前端·vue.js·uni-app
谷雨不太卷6 小时前
进程的状态码
java·前端·算法
打小就很皮...6 小时前
基于 Python + LangChain + RAG 的知识检索系统实战
前端·langchain·embedding·rag
whinc6 小时前
JavaScript技术周刊 2026年第17周
javascript
BJ-Giser6 小时前
Cesium 烟雾粒子特效
前端·可视化·cesium
空中海6 小时前
02 ArkTS 语言与工程规范
java·前端·spring