实现拍照和获取本地图片功能

本文介绍了一个使用 React Native 实现的图片选择组件,该组件提供了从本地图库选择图片和拍照两种方式来获取图片的功能,并展示了如何通过 Expo 的 ImagePicker API 进行图片的选择与预览。
/* @flow */
import React, { Component } from 'react';
import {
  View,
  Text,
  StyleSheet,
  Button,
  Image,
  TouchableOpacity,
  Dimensions,
  TextInput,
  ToastAndroid,
} from 'react-native';
const ImageUrl='http://192.168.127.1:8080';
import ActionSheet from 'react-native-actionsheet';


const options = [ '取消', '本地图库', '拍照'];
const title = '请选择';
export default class SetView extends Component {
  constructor(props) {
     super(props)
     this.state = {
       selected: '',
       image:'',
     }
     this.handlePress = this.handlePress.bind(this)
     this.showActionSheet = this.showActionSheet.bind(this)
   }
   showActionSheet() {
     this.ActionSheet.show()
   }
   handlePress(i) {
    if(i == 1){
      console.log('本地图册');
      Expo.ImagePicker.launchImageLibraryAsync()
      .then((info)=>{
        this.setState({image:info.uri});
        this.settingUserImage();
      })
    }else if(i == 2){
      console.log('拍照');
      Expo.ImagePicker.launchCameraAsync()
      .then((info)=>{
        this.setState({image:info.uri});
      })
    }
  }
 
  render() {
      return (
        <View style={styles.container}>
          <ActionSheet
             ref={o => this.ActionSheet = o}
             title={title}
             options={options}
             cancelButtonIndex={0}
             destructiveButtonIndex={3}
             onPress={this.handlePress}
           />
        </View>
      );
    }
  }
  const styles = StyleSheet.create({
    container: {
      flex: 1,
    },
  });

 

import ActionSheet from'react-native-actionsheet';

转载于:https://www.cnblogs.com/sunny-pei666/p/7349928.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值