gpt4 book ai didi

javascript - 为什么我的相机胶卷模式是空的?

转载 作者:行者123 更新时间:2023-12-03 02:32:08 25 4
gpt4 key购买 nike

我正在尝试按照 React Native 文档访问 Camera Roll 。我尝试将相机胶卷图像包装在 Modal Component 中。当我按下“打开相机胶卷”按钮时,模态从底部出现,“关闭模态”按钮可见,但相机胶卷中的图像没有

当我没有将它包裹在 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>
);
}
}

最佳答案

知道了,需要flex: 1对于 <ScrollView>风格和contentContainerStyle 。看起来不太好,但照片显示。感谢 Reddit 上的 u/Mingli91。

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


const Dimensions = require('Dimensions');
const window = Dimensions.get('window');
const screenWidth = window.width;
const screenHeight = window.height;


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 style={styles.modal}
visible={this.state.modalVisible}
animationType={'slide'}
onRequestClose={() => this.closeModal()}>
<ScrollView style={{flex: 1}}
contentContainerStyle={{ height: 100, width: 300 }}>
{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>
);
}
}

const styles = StyleSheet.create({
modal: {
flex: 1,
alignItems: 'center',
width: screenWidth,
height: screenHeight,
}
});

关于javascript - 为什么我的相机胶卷模式是空的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48694865/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com