React Native - 使用图片选择器react-native-image-picker拍照、选照片

我们程序中需要提供用户上传照片的功能。照片可以从设备相册中选择,也可以使用摄像头直接拍摄。这个功能使用 react-native-image-picker 库就可以很方便的实现。
1,react-native-image-picker介绍
react-native-image-picker 是一个第三方的开源库,它提供了原生的 UI 界面供用户选择图片或视频。图片或视频的来源可以是系统相簿,也可以是相机直接拍摄。
使用 react-native-image-picker 我们不必关心如何与设备相册、摄像头如何交互,用户操作完毕后我们就可以直接得到资源的 URI 地址、或者 base64 字符串(限图片)。
GitHub 主页地址:https://github.com/marcshilling/react-native-image-picker

2,安装配置
(1)首先在“终端”中进入项目目录,然后执行如下命令安装最新版本的 react-native-image-picker
1

npm install react-native-image-picker@latest --save

(2)接着运行如下命令链接相关的依赖库:

react-native link

(3)由于我们需要调用摄像头拍照、录像,同时拍完还要保存到相册中。因此需要在 Info.plist 里配置请求摄像头、麦克风、及照片的相关描述字段:

Privacy - Camera Usage Description
Privacy - Photo Library Usage Description
Privacy - Microphone Usage Description

使用

var ImagePicker = require('react-native-image-picker');
//图片选择器参数设置
var options = {
  title: '请选择图片来源',
  cancelButtonTitle:'取消',
  takePhotoButtonTitle:'拍照',
  chooseFromLibraryButtonTitle:'相册图片',
  customButtons: [
    {name: 'hangge', title: 'hangge.com图片'},
  ],
  storageOptions: {
    skipBackup: true,
    path: 'images'
  }
};
export default class App extends Component<Props> {
//构造器
constructor(props) {
  super(props);
  this.state = {
   avatarSource:require('/Users/ljl/RNTwoApp/22.jpg')

  };

 this.onPressPicker = this.onPressPicker.bind(this);
// this.NetINfo = this.NetINfo.bind(this);
}

render() {
    return (
      <View style={styles.container}>
       <Button onPress={this.onPressCamera}
      title="相机拍照摄像"/> 
       <Button onPress={this.onPressPhoto}
      title="相册图片"/> 
       <Button onPress={this.onPressPicker}
      title="拍照选照片"/> 
      <Button onPress={this.onPressLocition.bind(this)}
      title="定位"/> 
       <Button onPress={this.Vibration}
      title="实现震动"/> 
      <Button onPress={this.NetINfo}
      title="获取网络信息"/> 
    <Image source={this.state.avatarSource} style={styles.image}/>

      </View>
    );
  }

//拍照取照片
  onPressPicker(){
    ImagePicker.showImagePicker(options,(response) =>{
      console.log('response'+response);
      if (response.didCancel){
        console.log('用户取消了选择');
      }else if (response.error){
        alert("ImagePicker发生错误:" + response.error);

          }else if (response.customButton) {
                  alert("自定义按钮点击:" + response.customButton);
                }else {

                                        let source = {uri: response.uri.replace('file://', '')};

                                        // You can also display the image using data:
                                        // let source = { uri: 'data:image/jpeg;base64,' + response.data };

                                          this.setState({
                                           avatarSource: source
                                          })

                      }
     })      
  }
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,
  },
  image:{
    height:198,
    width:300,
    color: 'red',
    //alignSelf:'center',
  }
});
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
好的,关于React Native图片,我们可以使用第三方库react-native-image-picker来实现。以下是具体的实现步骤: 1. 安装react-native-image-picker使用npm或yarn命令进行安装: ``` npm install react-native-image-picker --save ``` 或者 ``` yarn add react-native-image-picker ``` 2. 导入库 在需要使用图片选择器的组件文件中导入库: ``` import ImagePicker from 'react-native-image-picker'; ``` 3. 添加图片选择器按钮 在需要添加图片选择器的地方添加一个按钮,点击该按钮会触发图片选择器的弹出: ``` <Button title="图片" onPress={this.selectImage} /> ``` 4. 实现图片选择器方法 定义一个selectImage方法,该方法将调用图片选择器,并且将择的图片传递给回调函数: ``` selectImage = () => { const options = { title: '图片', cancelButtonTitle: '取消', takePhotoButtonTitle: '拍照', chooseFromLibraryButtonTitle: '从相册中择', storageOptions: { skipBackup: true, path: 'images', }, allowsMultipleSelection: true, }; ImagePicker.showImagePicker(options, (response) => { if (response.didCancel) { console.log('用户取消了图片择'); } else if (response.error) { console.log('图片选择器出错:', response.error); } else if (response.customButton) { console.log('自定义按钮被点击:', response.customButton); } else { console.log('择的图片信息:', response); const { uri, fileName, type, fileSize } = response; // 将择的图片传递给其他组件处理 this.props.onImageSelected({ uri, fileName, type, fileSize }); } }); } ``` 在该方法中,我们定义了图片选择器的参数options,其中allowsMultipleSelection为true表示可以择多张图片。 然后调用ImagePicker.showImagePicker方法弹出图片选择器择的结果将传递给回调函数response,我们可以从response中获取择的图片的信息,然后将其传递给其他组件进行处理。 以上就是使用react-native-image-picker实现React Native图片的步骤。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值