具有大量图像的 React-native 动态图像 [英] React-native dynamic images with lots of images

查看:69
本文介绍了具有大量图像的 React-native 动态图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个由 150 个按钮组成的数组,这些按钮链接到 150 张图片,按下按钮后我需要显示图片.按钮的信息存储在 JSON 文件中.图片名称是按钮的 ID,所以 1.jpg、2.jpg 等.

I have an array of 150 buttons that link to 150 pictures, i need to show the picture once the button is pressed. The information for the buttons is stored in a JSON file. The pictures names are the ID's of the buttons, so 1.jpg, 2.jpg etc.

现在面临写不出来的问题:

Now I am facing the problem that I can't write:

 fish["image"] = {uri: "asset-library://" + fish.id + ".jpg"};

使用 if 语句的另一个解决方案不起作用,因为我有很多选择,有什么想法吗?

And the other solution with if statements does not work since I have so many options, any ideas?

非常感谢

推荐答案

我遇到了类似的问题.我构建了一个函数,其中包含一个巨大的 switch 语句,并且对每种情况都有静态需求.

I had a similar problem. I built a function that contains a huge switch statement and has static requires for every case.

function getImage(id) {
    switch(id) {
        case 1:
            return require('./img/1.jpg');
        case 2:
            return require('./img/2.jpg');
        ...
    }
}

现在可以了

fish["image"] = getImage(fish.id);

我还必须使用 100 多个图标,因此我没有手动编写案例,而是构建了一个自动生成函数的脚本.

I also had to use over 100 icons, so instead of writing the cases by hand, I built a script that generates the function automatically.

这篇关于具有大量图像的 React-native 动态图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆