【发布时间】:2018-02-08 21:12:34
【问题描述】:
我正在尝试按照 React Native 文档访问 Camera Roll。我尝试将相机胶卷图像包装在Modal Component 中。当我按下“打开相机胶卷”按钮时,Modal 从底部出现,“关闭模式”按钮可见,但 none 来自相机胶卷的图像。
当我没有将它包裹在 Modal 中时,图像会出现,但它们会破坏我屏幕上所有组件的布局,这就是为什么我(未成功)尝试将它放在 @ 987654325@.
另外,如果您能给我一些关于如何使图像可选择并以每行 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>
);
}
}
【问题讨论】:
-
尝试为 Modal 添加样式
-
嗯,我添加了
flex、justifyContent和backgroundColor,但仍然没有图像 -
尝试为 Modal 和 ScrollView 设置固定宽度和高度
-
仍然没有显示
-
你曾经调用过 getPhotos() 吗?您有一个触发
this.openModal()的按钮,但看起来您刚刚忘记从 openModal() 调用this.getPhotos()!
标签: javascript react-native modalviewcontroller camera-roll