31
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ReactNativeのImageで画像が表示されない

Last updated at Posted at 2017-09-24

ReactNativeのImageで画像が表示されなくてはまる

最初に結論

  • widthとheightを忘れずに!

Imageタグの使い方

  • source属性に画像のURLを設定する

① プロジェクト内に入ってるファイルを使う時

  • sourceにファイルをrequire/importしたものを渡す
<Image source={require('./sample.png')} />

② 外部から画像を取得する場合

  • uriプロパティにURLを設定したオブジェクトを渡す

<Image source={{ uri: 'https://facebook.github.io/react-native/img/header_logo.png' }} />

ハマりどころ

  • ①は特に問題なし
  • ②は上記の例だと表示されなくてはまる

解決策

  • widthとheightをつけると表示される
  • こんな感じ
<Image
    source={{ uri: 'https://facebook.github.io/react-native/img/header_logo.png' }}
    style={{ width: 66, height: 58 }}
/>

サンプル

import React from 'react';
import { AppRegistry, Image, View } from 'react-native';

const Sample = () => (
  <View>
    {/* これは問題なく表示される */}
    <Image
       source={require('./sample.png')}
       style={{ backgroundColor: 'lightgreen' }}
       />
    {/* これはwidth/heightがないので表示されない */}
    <Image
       source={{ uri: 'https://facebook.github.io/react-native/img/header_logo.png' }}
       style={{ backgroundColor: 'yellow' }}
       />
    {/* これはwidth/heightがあるので表示される */}
    <Image
       source={{ uri: 'https://facebook.github.io/react-native/img/header_logo.png' }}
       style={{ width: 66, height: 58, backgroundColor: 'lightblue' }}
       />
  </View>
);

AppRegistry.registerComponent('Sample', () => Sample);
  • こんな感じ

Screenshot_1506219453.png

まとめ

  • Imageタグ使う時はwidth/heightを忘れずに
31
15
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
31
15

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?