CameraButton.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import React from 'react'
  2. import {
  3. TouchableOpacity,
  4. StyleSheet,
  5. Platform,
  6. ActivityIndicator,
  7. View,
  8. Text,
  9. ToastAndroid
  10. } from 'react-native'
  11. //var ImagePicker = require('react-native-image-picker');
  12. //import Icon from 'react-native-vector-icons/Ionicons';
  13. const options = {
  14. title: '选择图片',
  15. cancelButtonTitle: '取消',
  16. takePhotoButtonTitle: '拍照',
  17. chooseFromLibraryButtonTitle: '图片库',
  18. cameraType: 'back',
  19. mediaType: 'photo',
  20. videoQuality: 'high',
  21. durationLimit: 10,
  22. maxWidth: 600,
  23. maxHeight: 600,
  24. aspectX: 2,
  25. aspectY: 1,
  26. quality: 0.8,
  27. angle: 0,
  28. allowsEditing: false,
  29. noData: false,
  30. storageOptions: {
  31. skipBackup: true,
  32. path: 'images'
  33. }
  34. };
  35. class CameraButton extends React.Component {
  36. constructor(props){
  37. super(props);
  38. this.state = {
  39. loading:false
  40. }
  41. }
  42. render() {
  43. const {photos,type} = this.props;
  44. // let conText;
  45. // if(photos.length > 0){
  46. // conText = (<View style={styles.countBox}>
  47. // <Text style={styles.count}>{photos.length}</Text>
  48. // </View>);
  49. // }
  50. return (
  51. <TouchableOpacity
  52. onPress={this.showImagePicker}
  53. style={[this.props.style,styles.cameraBtn]}>
  54. <View>
  55. {this.props.children}
  56. </View>
  57. </TouchableOpacity>
  58. )
  59. }
  60. //<Icon name="md-camera" color="#aaa" size={34}/>
  61. //{conText}
  62. showImagePicker = () => {
  63. ImagePicker.showImagePicker(options, (response) => {
  64. console.log('**************', response);
  65. if (response.didCancel) {
  66. console.log('User cancelled image picker');
  67. }
  68. else if (response.error) {
  69. console.log('ImagePicker Error: ', response.error);
  70. }else {
  71. let source;
  72. if (Platform.OS === 'android') {
  73. source = {uri: response.uri, isStatic: true}
  74. } else {
  75. source = {uri: response.uri.replace('file://', ''), isStatic: true}
  76. }
  77. let file;
  78. if(Platform.OS === 'android'){
  79. file = response.uri
  80. }else {
  81. file = response.uri.replace('file://', '')
  82. }
  83. // this.setState({
  84. // loading:true
  85. // });
  86. this.props.onFileUpload(file,response.fileName)
  87. // .then(result=>{
  88. // this.setState({
  89. // loading:false
  90. // })
  91. // })
  92. }
  93. });
  94. }
  95. }
  96. const styles = StyleSheet.create({
  97. cameraBtn: {
  98. padding:5
  99. },
  100. count:{
  101. color:'#fff',
  102. fontSize:12
  103. },
  104. fullBtn:{
  105. justifyContent:'center',
  106. alignItems:'center',
  107. backgroundColor:'#fff'
  108. },
  109. countBox:{
  110. position:'absolute',
  111. right:-5,
  112. top:-5,
  113. alignItems:'center',
  114. backgroundColor:'#34A853',
  115. width:16,
  116. height:16,
  117. borderRadius:8,
  118. justifyContent:'center'
  119. }
  120. });
  121. export default CameraButton;