首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >为什么在expo build:android之后图片不能在apk上显示?(React Native)

为什么在expo build:android之后图片不能在apk上显示?(React Native)
EN

Stack Overflow用户
提问于 2018-08-04 22:14:15
回答 1查看 5K关注 0票数 5

我创建了一个应用程序(使用React Native Expo),它有一些图像,如:

<Image source={require('../assets/facebook_button.png')} />

这些图像位于assets目录中,我可以使用npm publishnpm start功能查看它们。

问题是当使用exp build:android构建应用程序时,现在apk是而不是显示静态图像(但我从互联网上加载的其他一些图像正在显示)。

这是我的app.json:

代码语言:javascript
复制
{
  "expo": {
    "name": "my-interesting-app",
    "description": "My Interesting App",
    "slug": "my-interesting-app",
    "privacy": "public",
    "sdkVersion": "29.0.0",
    "platforms": ["ios", "android"],
    "version": "1.0.0",
    "orientation": "portrait",
    "icon": "./assets/icon.png",
    "splash": {
      "image": "./assets/splash.png",
      "resizeMode": "contain",
      "backgroundColor": "#ffffff"
    },
    "updates": {
      "fallbackToCacheTimeout": 0
    },
    "assetBundlePatterns": [
      "**/*",
      "assets/*"
    ],
    "ios": {
      "supportsTablet": true,
      "bundleIdentifier": "com.myinteresting.myinterestingapp"
    },
    "android": {
      "package": "com.myinteresting.myinterestingapp"
    },
    
  }
}

谢谢你的帮助

EN

回答 1

Stack Overflow用户

发布于 2018-08-13 19:55:05

在加载之前尝试缓存图像。

代码语言:javascript
复制
import React from 'react';
import { Image, Text, View } from 'react-native';
import { Asset, AppLoading } from 'expo';

export default class App extends React.Component {
  state = {
    isReady: false,
  };

  render() {
    if (!this.state.isReady) {
      return (
        <AppLoading
          startAsync={() => this._cacheResourcesAsync()}
          onFinish={() => this.setState({ isReady: true })}
          onError={console.warn}
        />
      );
    }

    return (
      <View style={{ flex: 1 }}>
        <Image source={require('./assets/images/expo-icon.png')} />
        <Image source={require('./assets/images/slack-icon.png')} />
      </View>
    );
  }

  async _cacheResourcesAsync() {
    return Asset.loadAsync([
      require('./assets/images/expo-icon.png'),
      require('./assets/images/slack-icon.png'),
    ]);
  }
}

参考文献:

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51686444

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档