【问题标题】:ReactNative Expo Preloading & Caching ImagesReact Native Expo 预加载和缓存图像
【发布时间】:2019-09-04 14:27:07
【问题描述】:

我是 react-native 的新手,我试图在应用程序开始时预加载 10 张图片,我遵循了世博会文档,但是

我想缓存来自外部文件的图像,但它给了我一个错误 [Un Handeled Promise Rejection]

这是我的 entry.js

export const ENTRIES1 = [
    {
        title: 'Makeup Artists',
        illustration: require('../assets/img/makeup.png')
    },
    {
        title: 'Photographers',
        illustration: require('../assets/img/Photographers.png')
    },
    {
        title: 'Wedding Planners',
        illustration: require('../assets/img/weddingPlanner.jpg')
    },
    {
        title: 'Wedding Halls',
        illustration: require('../assets/img/wedding-Hall.png')
    },
    {
        title: 'Laser & Beauty Centers',
        illustration: require('../assets/img/laser.png')
    },
]

loadingScreen.js

      async componentDidMount() { //Preload Fonts
    await Asset.loadAsync(ENTRIES1.illustration),
    await Font.loadAsync({
        'Roboto': require('../../node_modules/native-base/Fonts/Roboto.ttf'),
        'Roboto_medium': require('../../node_modules/native-base/Fonts/Roboto_medium.ttf'),
      ...Ionicons.font,
    });
    this.checkIfLoggedIn();
  }

我做错了什么?谢谢

【问题讨论】:

    标签: reactjs react-native expo


    【解决方案1】:

    试试这个:)

    function cacheImages(images) {
      return images.map(image => {
        if (typeof image.illustration === 'string') {
          return Image.prefetch(image.illustration);
        } else {
          return Asset.fromModule(image.illustration).downloadAsync();
        }
      });
    }
    
     async componentDidMount() { 
    
    
        await Asset.cacheImages(ENTRIES1),
        await Font.loadAsync({
            'Roboto': require('../../node_modules/native-base/Fonts/Roboto.ttf'),
            'Roboto_medium': require('../../node_modules/native-base/Fonts/Roboto_medium.ttf'),
          ...Ionicons.font,
        });
        this.checkIfLoggedIn();
      }
    

    【讨论】:

    • 本地图片 url 是正确的,我在主屏幕上使用它们,但我想在用户进入主屏幕之前应用程序启动时预加载它们
    • 感谢您的回答我试过了,但我得到了[Unhandled promise rejection: TypeError: _expoAsset.Asset.cacheImages is not a function. (In '_expoAsset.Asset.cacheImages(_entries.ENTRIES1)', '_expoAsset.Asset.cacheImages' is undefined)]
    • 您从 'expo-asset' 导入了 import { Asset }; ?
    • 我确实导入了它
    猜你喜欢
    • 2020-06-22
    • 2016-05-17
    • 1970-01-01
    • 2019-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    相关资源
    最近更新 更多