无法在本机中上传/启动图片上传器

时间:2019-07-24 11:53:59

标签: javascript reactjs react-native

在开始显示代码之前,我想提到我正在使用iPhone模拟器。

所以,我正在经历这个blog

添加了这样的权限(在我的info.plist中)

<key>NSPhotoLibraryUsageDescription</key>
<string>For choosing a photo.</string>
<key>NSCameraUsageDescription</key>
<string>For taking a photo.</string>
<key>NSPhotoLibraryAddUsageDescription</key>
<string>For saving a photo.</string>

其次是按字面上复制并粘贴其代码

import React, { Component } from 'react'
import { 
  View,
  Image,
  Button,
  Text
 } from 'react-native'
import ImagePicker from 'react-native-image-picker'
class UserImage extends Component {

  state = {
    photo: null
  }

  handleChosePhoto = () => {
    const options = {
      noData: true
    }

    ImagePicker.launchImageLibrary(options, response => {
      if (response.uri) {
        this.setState({ photo: response });
      }
    });
  }

  render () {
    return (
      <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      {this.state.photo && (
        <Image
          source={{ uri: photo.uri }}
          style={{ width: 300, height: 300 }}
        />
      )}
      <Button title="Choose Photo" onPress={this.handleChosePhoto} />
    </View>
    )
  }

}

export default UserImage

现在,当我单击选择的照片时,它将带我到Xcode屏幕,该屏幕将被打开

enter image description here

有人可以帮助我弄清楚我可能做错了什么吗?还是可以在模拟器上使用?

1 个答案:

答案 0 :(得分:0)

将函数名称从handleChosePhoto更改为handleChoosePhoto