功能描述:点击按钮 “上传图片” 后,从本地相册中选择一张图片,然后在当前界面展示。
遇到问题:图片无法展示,且报错如下:
报错:[渲染层网络层错误] Failed to load image http://127.0.0.1:34445/__tmp__/Znv7wjLk1U7qe60af28e46db53e9c9bb610a3444adc2.jpg
the server responded with a status of 500 (HTTP/1.1 500 Internal Server Error)
From server 127.0.0.1(env: macOS,mp,1.06.2412040; lib: 3.7.5)
代码如下:
index.wxml
<!-- pages/index/index.wxml -->
<view class="container">
<!-- 添加图片上传按钮 -->
<button bindtap="chooseImage">选择图片</button>
</view>
<!-- 添加图片预览区域 -->
<view class="image-container">
<image wx:if="{{imagePath}}" src="{{imagePath}}" mode="aspectFit" class="preview-image"></image>
<image wx:else src="./images/default.png" mode="aspectFit" class="default-image"></image>
</view>
<view>
index.js
// index.js
Page({
data: {
imagePath: '', // 原始图片路径
processedImagePath: '', // 处理后图片路径
targetWidth: '', // 用户输入的目标宽度
targetSize: '', // 用户输入的目标存储大小
},
// 选择图片
chooseImage() {
wx.chooseMedia({
count: 1,
mediaType: ['image'],
success: (res) => {
const tempFilePath = res.tempFiles[0].tempFilePath;
wx.getImageInfo({
src: tempFilePath,
success: (imageInfo) => {
console.log(imageInfo.path)
this.setData({ imagePath: imageInfo.path });
this.saveImageToLocal(tempFilePath);
},
fail: (err) => {
console.log("获取图片信息失败", err);
}
});
},
fail: (res) => {
console.log("error")
}
});
},
// 保存图片到小程序本地文件系统
saveImageToLocal(filePath) {
const that = this;
wx.saveFile({
tempFilePath: filePath,
success: function (res) {
const savedFilePath = res.savedFilePath;
console.log('图片已保存到本地:', savedFilePath);
that.setData({ imagePath: savedFilePath });
},
fail: function (err) {
console.log("保存图片失败", err);
wx.showToast({
title: '保存失败',
icon: 'none'
});
}
});
},

能显示啊,是不是没重新编译
报错:[渲染层网络层错误] Failed to load image http://127.0.0.1:34445/__tmp__/Znv7wjLk1U7qe60af28e46db53e9c9bb610a3444adc2.jpg
the server responded with a status of 500 (HTTP/1.1 500 Internal Server Error)
From server 127.0.0.1(env: macOS,mp,1.06.2412040; lib: 3.7.5)
chooseImage 这个方法里面的this在外面定义一下,就像你下面这个方法的that一样