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
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
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
3. Solution
At this time, I re-read the instructions on using the
Through this explanation, I understand that it is because the
uni-app
Native componentsimage
are based on # when usingweb
containers (includingH5
,App
) ##HTMLimg
Implemented by component. The
srcattribute of the
imagetag cannot be directly adapted to the local path and requires special processing:
- HBuilderX
When debugging on a real machine, setdevelopment tool page, Set the
Appstartup mode to
Customor
Customized debugging, Function-> Run-> App startup mode-> Custom (debug) ` , then just like the browser, both local paths and network paths can be used.
- app-plus
in
manifest.json->
debug->
webviewis
true. In this way, when debugging on a real machine, you can also directly use the local path to access.
img component in
HTML, in # Direct use of local paths is not supported in ##uniapp
and requires special processing. So I used the
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"><template>
<view>
<image></image>
</view>
</template>
<script>
export default {
data() {
return {
imageList: [] // 图片列表
}
},
methods: {
async getImageList() {
const { tempFilePaths } = await uni.chooseImage({ // 从相册中选择图片
count: 9,
sizeType: [&#39;original&#39;, &#39;compressed&#39;],
sourceType: [&#39;album&#39;],
})
for (let i = 0; i < tempFilePaths.length; i++) {
this.imageList.push({
path: tempFilePaths[i],
})
}
}
},
mounted() {
this.getImageList()
}
}
</script></pre>
Finally, after this investigation, I understood the reason why the
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!

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

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

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

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

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

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

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.

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.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

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 latest version

Notepad++7.3.1
Easy-to-use and free code editor

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6
Visual web development tools
