uniapp微信小程序无法使用本地静态资源图片(背景图在真机不显示)的解决方法

手机APP/开发
853
0
0
2023-07-12
目录
  • 前言
  • 先看uniapp官网对背景图片的说明
  • 错误用法
  • 解决方式
  • 动态将图片转base64
  • 手动将图片转base64作为参数使用
  • 总结

前言

首先要说明,使用HBuilder或者vs Code工具开发的时候,在微信开发者工具调试的时候,我们使用本地图片是OK的,但是一旦放到真机上调试的时候,图片就显示不出来。

先看uniapp官网对背景图片的说明

错误用法

<template>
  <view class="home" :style="{backgroundImage:`url(${homeBg})`">
    <!--你的内容-->
  </view>
</template>
 
<script setup>
  import homeBg from "@/static/img/home/home_bg.png"
  const homeBg = homeBg 
</script>
 
<style lang="scss">
</style>

或者

<template>
  <view class="home">
    <!--你的内容-->
  </view>
</template>
 
<script setup>
 
</script>
 
<style lang="scss">
 .home {
	background-image: url('~@/static/img/home/home_bg.png');
  }
</style>

解决方式

将图片资源放到服务器,以https://形式使用
将图片转为base64格式使用
background使用image标签替代

动态将图片转base64

在工具文件夹创建common.js文件

//解决小程序端真机图片不展示
 
/**
 * 动态绑定的使用方式 例如首页使用:
 * import common from '@/utils/common'
 * :style="{
			backgroundImage: `url(${common.urlTobase('home', 'home-bg')})`,
	 }"
 */
 
/**
 * 获取本地图
 * @param folder // 文件夹名字 如 /static/images/home
 * @param fileName // 文件名 如 home-bg
 * @param format // 文件类型 如 png jpg
 * @returns {*|string}
 */
 
// #ifdef MP-WEIXIN
const urlTobase = (folder, fileName, format = "png") => {
  let img = `/static/images/${folder}/${fileName}.${format}`,
    imgBase = wx.getFileSystemManager().readFileSync(img, "base64"),
    baseUrl = `data:image/png;base64,${imgBase64}`;
  /*   console.log(
    "data:image/png;base," + imgBase64,
    "图片转换成功+++++++++++++++++++++++++++++"
  ); */
 
  return baseUrl;
};
// #endif
 
export default {
  urlTobase,
};

在vue文件中使用方式

<template>
  <view class="home" :style="{backgroundImage:`url(${common.urlTobase('home', 'home-bg')})`">
    <!--你的内容-->
  </view>
</template>
 
<script setup>
  import common from '@/utils/common'
</script>
 
<style lang="scss">
</style>

手动将图片转base64作为参数使用

在静态资源文件夹下创建一个sass文件,如base64-img-store.scss文件
图片在线转换base64编码-在线base64编码转换成图片工具 http://www.jsons.cn/img2base64/

// 解决小程序端真机图片不展示
 
/* *
 * 需要在根目录下uni.scss下全局引入
 * 在css中的使用方式 例如首页使用:
 * .home{
 *   background-image: $home_bg_base_code;
 *   background-size: cover;
 *  }
 * 图片在线转换base编码-在线base64编码转换成图片工具 http://www.jsons.cn/img2base64/
 * 将图片转换好的base放到对应url里
 * 样式变量名可以自行定义 例如:$home_bg_base_code$a$bg
 */
 
$home_bg_base_code: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAXgAAADICAYAAADiIPpnAAAAAXNSR0IArs4c6QAAGq5JREFUeNrt3UmWrDqSBmAzA4+4r5rBW0Dup3ZVub3cTQ1qUOdEuKwGIFx4C0KNSfyc8zLiRjoOqDF9CCHxf/3rX/9NRP+krZt++gC//pdS/Kavj3HsfJ/tw7t34c3H4gTnt+F7k6QDERMnOLcPaZEsDTakhe4uQQXLHh+rJ7q7Rqapm2/3z1xXD9Zhzla2C9XR99s/JflXZjjL4sfgEtcQU8Le7aNZjqN705prpoGtUlr8rPjAMbhCOc7xHS1vGa5fcp+EproAtl1o9GgFO+Wm5b+XI44eextqpMwWK3FsqLxvRJoWzZfyaSFtVVwoHorvXXwNK96oYs+seClxElA8FA/FN6L4LF01UHyttJD2Ki4UD8VD8dkU36Fiz6x4KXUSUDwUD8VbU3zJB65a59pPrnhps+LaVrxSwQoNxUPxUDAUnyXAQ/H93iZD8VA8FN+84qVqxWXrwQGKh+KheIi4Uv4mOGc5HOK5aJGF4qH4kyleofiCZUurnFtuwXPFigvFQ/EtKr4VHUPxh+pw64qXGoWoWcVnCzotKl77+V4+enQovg3FZyiBxhtQKXeiHSieIhVfVbC5FG9U5FA8FA/FJxa8KcVr3kLPlTIZiofioXgo/lCA70Lx9oJa9MtPUDwUD8VD8Qfy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xIpr3ZdPh+8wKPbDjllGvES1O+7sfjZz6C7g5WWNStvn+Pgc0+WSHt6gi2u3Zru1niQuZvnbvNdPaH2nSo5XmU3FJ9pG4Lpkv1dGXOhzkkDin95Z2+m7crx8lOmQv204LxsKDj6kOFKRsI8Bemd39GWEO0ofv9t/+s3XnW1/NzUAGjQEGhTeVRH8f69Bn/HGE4Vsdw5vhNShc3Ce/79Ct6MCD9nMxMtXUsD73w9vngaZi62JfOskEbfDR4I7/6chs+J1gtSb2oIGlX8bZ3X23Ob2+/hqlPceKwpr/hMAb5/xUffSgba85U+11wn1QVhXfGGun5uiy/fd73x08Zgek7kGwJanhHdPy+6dReu/5ayXD92C66f06yuUdcLc39euITan6OmpuD/Hr/+7X+vv+u+w+AWsvmX0Spk9DIEcP65vCLPTNxFGkLxVGlEzaoMPVuUekf3hPqTCh4m6YZA7o/Lnz8ZfW79xpqyih8HERpYaODXBUyDoO/HDk8jT3StiM4V70eVSDCaRMJbSno3B3idOWqg+DYUXytrVk/ek6UM5qixUuLGz2l06wvbcpYavCiiAQ7CW0kK/h1+ju7uGFLlDd89rPH68LH4NoIkHPIXDgs8OgSwg8ACxZtRfLrjBHloeZI0KD5fgN8fTPm2ri0fMMHD0MTP93nhK1DZnqo3+HYrFA/FN+9WKN5GgM8WI/hzd4f9+eKheCgeiofiyyleygS1yEQqc6Qi51b7HLT2tXPBsmOrtJxc8bnKvLZVXyuVODlVeeOzZnPD18PGyweXuyalXG+6afn07r2HjA/UnYRpI6aDABQPxUPxAAAUD8FD8VA8FA/FQ/HVAjwUD8VD8edsqKH4WiVOTlmOofgGFa+2y0cXiq9QR6D4rIr/f7YIFM2cex0MAAAAAElFTkSuQmCC);

然后在根目录下uni.scss下引入

@import "@/static/style/common/base-img-store.scss";

在vue文件中使用方式

<template>
  <view class="home">
    <!--你的内容-->
  </view>
</template>
 
<script setup>	
 
</script>
 
<style lang="scss">
 .home {
	background-image: $home_bg_base_code;
    background-size: cover;
  }
</style>

到此结束。