我正在学习react-nativv v0.56和 react 社区的 react-native-image-picker ( this )。我正在尝试使用TypeScript在 iOS 上实现他的图像选择器。A 遵循所有安装说明,但出现错误。现在我正在尝试使用Promise来实现它。这是我正在尝试的,在控制台调试器中没有任何错误。这是我的实验代码:
import * as React from 'react';
import { StyleSheet, Text, View, TouchableOpacity, Image, ImageSourcePropType, ImageURISource } from 'react-native';
import { Navigation } from 'react-native-navigation';
import Icon from 'react-native-vector-icons/FontAwesome';
import ImagePicker from 'react-native-image-picker';
import { ScreenID } from '../screenID'
export interface IWelcomeScreenProps {
navigator: Navigator;
componentId: string;
}
export interface IWelcomeScreenState {
pickedImage: ImageURISource;
}
export class WelcomeScreen extends React.Component<IWelcomeScreenProps, IWelcomeScreenState> {
constructor(props: IWelcomeScreenProps)
{
super(props);
this.state = {
pickedImage : {
uri: undefined
}
};
Navigation.events().bindComponent(this);
this.pushMapsScreen = this.pushMapsScreen.bind(this);
}
async pushMapsScreen()
{
await Navigation.push(this.props.componentId, {
component: {
name: ScreenID.mapScreen
}
})
}
async pickImageHandler()
{
return new Promise<ImageURISource>( (resolve, reject) =>
{
ImagePicker.showImagePicker({
title: 'Select Image'
},
result =>
{
if(result.didCancel)
{
reject('Image pick canceled!')
}
else if(result.error)
{
reject(result.error)
}
else
{
resolve(result);
}
});
}).then(x => {
this.setState({
...this.state,
pickedImage: {uri: x.uri}
})
}).catch(x => {
new Error(x.error)
});
}
//static declaration of the layout of the this screen
static get options()
{
return {
topBar:
{
title:
{
text: 'Mapporia Routes',
fontSize: 20,
color: 'red'
}
}
}
}
render() {
return (
<View style={styles.container}>
<Text style={styles.welcome}>
Welcome to Mapporia Routes!
</Text>
<TouchableOpacity onPress={ this.pushMapsScreen }>
<Icon name="map" size={200} color="#900"/>
</TouchableOpacity>
<Text style={styles.instructions}>
Pannonic IT
</Text>
<Image source={this.state.pickedImage} style={styles.previewImage} />
<TouchableOpacity style={ styles.button } onPress={this.pickImageHandler}>
<Text style={styles.buttonText}>PICK IMAGE</Text>
</TouchableOpacity>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
fontSize:30
},
button:{
marginTop: 20,
backgroundColor: 'blue',
padding:10
},
buttonText:{
color: 'white',
fontSize: 20
},
previewImage:{
width: 250,
height: 160,
backgroundColor: 'black'
}
});