search
HomeWeb Front-enduni-appHow to solve the problem of uniapp traversing pictures but not coming out?

Recently, when I was developing a small program using uniapp, I encountered a problem, that is, the pictures could not be displayed normally when traversing the pictures. After some exploration and research, I finally found a solution, and I'm sharing it with you now.

1. Problem description

In the small program I developed, I need to traverse all the pictures in a folder and display them on the page. First, I used uniapp's native component to display pictures. The specific code is as follows:

<pre class="brush:php;toolbar:false">&lt;template&gt;   &lt;view&gt;     &lt;image&gt;&lt;/image&gt;   &lt;/view&gt; &lt;/template&gt; &lt;script&gt; export default { data() { return { imageList: [] // 图片列表 } }, methods: { async getImageList() { const { tempFilePaths } = await uni.chooseImage({ // 从相册中选择图片 count: 9, sizeType: [&amp;#39;original&amp;#39;, &amp;#39;compressed&amp;#39;], sourceType: [&amp;#39;album&amp;#39;], }) for (let i = 0; i &lt; tempFilePaths.length; i++) { this.imageList.push({ path: tempFilePaths[i], }) } } }, mounted() { this.getImageList() } } &lt;/script&gt;</pre>

This code simply selects from the album by calling the uni.chooseImage() method Picture, then put the selected picture path into an array, and then display each picture through the v-for command.

But when I ran the program, no pictures were displayed on the page, so I started a long troubleshooting journey.

2. Troubleshooting

1. Is the image path correct?

First of all, I thought about the problem of the image path. I used the debugging tool of uniapp to check and found that there was no problem with the path, and the image could be displayed normally in the preview area of ​​the debugging tool.

2. Are the images loaded correctly?

The second question is whether the image is loaded correctly. Because I am using a local image, I tried to upload the image to the server and found that the image can be displayed normally on the server, but it still cannot be displayed locally.

3. Does it conflict with other components?

I suspect that it is because I use other components in the page that conflict with the tag, but after I comment out the code of other components, the image still cannot be displayed.

4. Can the picture browsing component provided by uniapp be displayed normally?

Finally I thought of using the image browsing component provided by uniapp<uni-image-preview></uni-image-preview> to preview the image, and found that the rendered image could be displayed normally.

Up to this point, I still haven’t found the source of the problem, but I am convinced that it is a problem with the tag.

3. Solution

At this time, I re-read the instructions on using the tag in the official uniapp documentation and discovered the problem:

uni-app Native components image are based on # when using web containers (including H5, App) ##HTML img Implemented by component. The src attribute of the image tag cannot be directly adapted to the local path and requires special processing:

  • HBuilderX development tool page, Set the App startup mode to Custom or Customized debugging, Function-> Run-> App startup mode-> Custom (debug) ` , then just like the browser, both local paths and network paths can be used.
  • When debugging on a real machine, set
  • app-plus in manifest.json -> debug -> webview is true. In this way, when debugging on a real machine, you can also directly use the local path to access.
Through this explanation, I understand that it is because the tag uses the

img component in HTML, in # Direct use of local paths is not supported in ##uniapp and requires special processing. So I used the

HBuilderX

development tools page to set the App startup mode to custom (debug), and now the pictures can be displayed normally. <pre class="brush:php;toolbar:false">&lt;template&gt;   &lt;view&gt;     &lt;image&gt;&lt;/image&gt;   &lt;/view&gt; &lt;/template&gt; &lt;script&gt; export default { data() { return { imageList: [] // 图片列表 } }, methods: { async getImageList() { const { tempFilePaths } = await uni.chooseImage({ // 从相册中选择图片 count: 9, sizeType: [&amp;#39;original&amp;#39;, &amp;#39;compressed&amp;#39;], sourceType: [&amp;#39;album&amp;#39;], }) for (let i = 0; i &lt; tempFilePaths.length; i++) { this.imageList.push({ path: tempFilePaths[i], }) } } }, mounted() { this.getImageList() } } &lt;/script&gt;</pre>Finally, after this investigation, I understood the reason why the tag cannot directly use the local path, and got the correct solution. I hope my experience can help students with similar problems. .

The above is the detailed content of How to solve the problem of uniapp traversing pictures but not coming out?. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do I handle local storage in uni-app?How do I handle local storage in uni-app?Mar 11, 2025 pm 07:12 PM

This article details uni-app's local storage APIs (uni.setStorageSync(), uni.getStorageSync(), and their async counterparts), emphasizing best practices like using descriptive keys, limiting data size, and handling JSON parsing. It stresses that lo

How to rename UniApp download filesHow to rename UniApp download filesMar 04, 2025 pm 03:43 PM

This article details workarounds for renaming downloaded files in UniApp, lacking direct API support. Android/iOS require native plugins for post-download renaming, while H5 solutions are limited to suggesting filenames. The process involves tempor

How to handle file encoding with UniApp downloadHow to handle file encoding with UniApp downloadMar 04, 2025 pm 03:32 PM

This article addresses file encoding issues in UniApp downloads. It emphasizes the importance of server-side Content-Type headers and using JavaScript's TextDecoder for client-side decoding based on these headers. Solutions for common encoding prob

How do I use uni-app's geolocation APIs?How do I use uni-app's geolocation APIs?Mar 11, 2025 pm 07:14 PM

This article details uni-app's geolocation APIs, focusing on uni.getLocation(). It addresses common pitfalls like incorrect coordinate systems (gcj02 vs. wgs84) and permission issues. Improving location accuracy via averaging readings and handling

How do I manage state in uni-app using Vuex or Pinia?How do I manage state in uni-app using Vuex or Pinia?Mar 11, 2025 pm 07:08 PM

This article compares Vuex and Pinia for state management in uni-app. It details their features, implementation, and best practices, highlighting Pinia's simplicity versus Vuex's structure. The choice depends on project complexity, with Pinia suita

How do I make API requests and handle data in uni-app?How do I make API requests and handle data in uni-app?Mar 11, 2025 pm 07:09 PM

This article details making and securing API requests within uni-app using uni.request or Axios. It covers handling JSON responses, best security practices (HTTPS, authentication, input validation), troubleshooting failures (network issues, CORS, s

How do I use uni-app's social sharing APIs?How do I use uni-app's social sharing APIs?Mar 13, 2025 pm 06:30 PM

The article details how to integrate social sharing into uni-app projects using uni.share API, covering setup, configuration, and testing across platforms like WeChat and Weibo.

How do I use uni-app's easycom feature for automatic component registration?How do I use uni-app's easycom feature for automatic component registration?Mar 11, 2025 pm 07:11 PM

This article explains uni-app's easycom feature, automating component registration. It details configuration, including autoscan and custom component mapping, highlighting benefits like reduced boilerplate, improved speed, and enhanced readability.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools