我正在使用react-native-image-picker
完全按照https://github.com/marcshilling/react-native-image-picker中所示的示例,通過終端使用react-native run-ios
並通過Atom編輯器進行編碼。反應本機圖像選擇器:無法讀取未定義的屬性'showImagePicker'
我做npm install [email protected] --save
和目前的package.json依賴性顯示:"react-native-image-picker": "^0.22.8",
而且我點擊了一個按鈕來觸發imagePicker
但我得到的錯誤:Cannot read property 'showImagePicker' of undefined
。
我在做什麼錯?
下面是代碼
import ImagePicker from 'react-native-image-picker'
var Platform = require('react-native').Platform
var options = {
title: 'Select Avatar',
customButtons: [
{name: 'fb', title: 'Choose Photo from Facebook'},
],
storageOptions: {
skipBackup: true,
path: 'images'
}
}
export default class chooseImage extends Component {
constructor() {
super()
this.state = {
avatarSource: "",
}
}
_uploadImage() {
ImagePicker.showImagePicker(options, (response) => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled image picker');
}
else if (response.error) {
console.log('ImagePicker Error: ', response.error);
}
else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
}
else {
// You can display the image using either data...
const source = {uri: 'data:image/jpeg;base64,' + response.data, isStatic: true};
// or a reference to the platform specific asset location
if (Platform.OS === 'ios') {
const source = {uri: response.uri.replace('file://', ''), isStatic: true};
} else {
const source = {uri: response.uri, isStatic: true};
}
this.setState({
avatarSource: source
})
}
})
}
render() {
return (
<View style={styles.container}>
<TouchableHighlight
onPress={this._uploadImage}
underlayColor='transparent'
>
<Image
source={this.state.avatarSource} style={styles.uploadAvatar}
/>
</TouchableHighlight>
</View>
)
}
}
編輯
當我CONSOLE.LOG(ImagePicker)
你是否在平臺特定的安裝的東西,如:https://github.com/marcshilling/react-native-image-picker#ios,和:https://github.com/marcshilling/react-native-圖像選擇器#android – Cherniv
@Cherniv我正在通過Atom編輯器和終端,而不是xcode做React Native。在我的另一個項目中,我所要做的只是npm安裝,並且工作。 –