通过canvas作图片缓存
测试环境在uniapp中
<template>
<view class="content">
<image class="logo" src="/static/logo.png"></image>
<view class="text-area">
<text class="title">{{title}}</text>
<view class="">
<image :src="imgSrc" mode=""></image>
</view>
</view>
</view>
</template>
<script>
export default {
data() {
return {
title: 'Hello',
imgSrc: '/static/logo.png'
}
},
onLoad() {
var that = this;
that.loadPic();
},
methods: {
loadPic(){
var that = this;
var url = "https://n.sinaimg.cn/spider20210721/200/w640h360/20210721/e9be-15a5a455644ce32c9bbfc8103a65ae39.jpg";
uni.getImageInfo({
src: url,
success(res) {
let img = new Image();
img.setAttribute("crossOrigin",'Anonymous')
img.src = res.path;
console.log(img)
let canvas = document.createElement('canvas');
var ctx = canvas.getContext("2d");
img.onload = function(e){
var path = img;
ctx.drawImage(path, 0, 0)
var baseStr = canvas.toDataURL("image/jpeg", 1);
that.imgSrc = baseStr;
}
}
});
}
}
}
</script>
<style>
.content {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-left: auto;
margin-right: auto;
margin-bottom: 50rpx;
}
.text-area {
display: flex;
justify-content: center;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>