1

Camera Rollへのアクセスに関する React Native ドキュメントに従おうとしています。カメラロールの画像をModal Componentでラップしてみました。「カメラロールを開く」ボタンを押すModalと、「モーダルを閉じる」ボタンが表示された状態で下から表示されますが、カメラロールの画像は表示されません。

でラップしていなかった場合Modal、画像は表示されますが、画面上のすべてのコンポーネントのレイアウトが台無しになりますModal

また、画像を選択可能にし、3 つの画像/行で表示する方法についてのヒントを教えていただければ幸いです。

    import React from 'react';
    import { CameraRoll, Image, Modal, ScrollView, Text, View } from 'react-native';
    import { Button } from 'react-native-elements';

    export default class AddEvent extends React.Component {
        constructor(props){
            super(props)
            this.state = {
                modalVisible: false,
                photos: [],
            }
        }

        getPhotos = () => {
            CameraRoll.getPhotos({
                first: 100,
            })
            .then(r => this.setState({ photos: r.edges }))
            .catch((err) => {
                alert('Error loading camera roll');
                return;
            });
        }

        openModal() {
            this.getPhotos();
            this.setState({modalVisible:true});
        }

        closeModal() {this.setState({modalVisible:false});}

        static navigationOptions = ({ navigation }) => {
            return {
                headerTitle: (<Text>Camera Roll Test</Text>),
            }
        };

        render(){
        return (
            <View>
                <Modal
                    visible={this.state.modalVisible}
                    animationType={'slide'}
                    onRequestClose={() => this.closeModal()}>
                    <ScrollView>
                        {this.state.photos.map((p, i) => {
                            return (
                                <Image
                                    key={i}
                                    style={{width: 300, height: 100,}}
                                    source={{ uri: p.node.image.uri }}/>
                            );
                        })}
                    </ScrollView>
                    <Button
                        onPress={() => this.closeModal()}
                        title="Close modal"/>
                </Modal>
                <Button
                    onPress={() => this.openModal()}/>
            </View>
        );
    }
}
4

2 に答える 2