MultiCameraButton.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. import React from 'react'
  2. import {
  3. TouchableOpacity,
  4. StyleSheet,
  5. Platform,
  6. View,
  7. } from 'react-native'
  8. import SyanImagePicker from 'react-native-syan-image-picker-zzly'
  9. class MultiCameraButton extends React.Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. loading: false,
  14. visible: false,
  15. }
  16. }
  17. render() {
  18. return (
  19. <View>
  20. <TouchableOpacity
  21. onPress={() => this.showImagePicker()}
  22. style={[this.props.style, styles.cameraBtn]}>
  23. <View>
  24. {this.props.children}
  25. </View>
  26. </TouchableOpacity>
  27. </View>
  28. )
  29. }
  30. showImagePicker = () => {
  31. const options = {
  32. isOpenCheckNumStyle: true,
  33. isWeChatStyle: true,
  34. imageCount: this.props.maxFiles, // 最大选择图片数目,默认6
  35. isCrop:false, // 是否允许裁剪,imageCount 为1才生效
  36. };
  37. SyanImagePicker.showImagePicker(options, (err, selectedPhotos) => {
  38. if (err) {
  39. // 取消选择
  40. return;
  41. }
  42. // 选择成功,渲染图片
  43. this.setState({visible: false,})
  44. for (const {uri} of selectedPhotos) {
  45. let file;
  46. if (Platform.OS === 'android') {
  47. file = uri
  48. } else {
  49. file = uri.replace('file://', '')
  50. }
  51. const file_list = file.split('/');
  52. let fileArr = file.split('.');
  53. let fileName = file_list[file_list.length - 1]
  54. let mime = `.${fileArr[fileArr.length - 1]}`
  55. if (mime.indexOf('mmexport') > -1) {
  56. mime = '.jpg'
  57. fileName = fileName.split('.')[0] + mime
  58. }
  59. this.props.photos.unshift(
  60. {
  61. img_id: '',
  62. type: 'img',
  63. mime: mime,
  64. uri: file,
  65. fileName: fileName
  66. }
  67. )
  68. }
  69. const itemID = this.props.itemID;
  70. if (this.props.maxFiles) {
  71. if (this.props.photos.length > this.props.maxFiles) {
  72. // Toast.info('仅能上传' + this.props.maxFiles + '张照片')
  73. const photos = this.props.photos.splice(0,this.props.maxFiles)
  74. this.props.onFileUpload(photos,itemID)
  75. } else {
  76. this.props.onFileUpload(this.props.photos,itemID)
  77. }
  78. }
  79. })
  80. }
  81. // showImagePicker1 = () => {
  82. // ImagePicker.openPicker({
  83. // mediaType: 'photo',
  84. // multiple: true,
  85. // }).then(images => {
  86. // this.setState({visible: false,})
  87. // for (const {path} of images) {
  88. // let file;
  89. // if (Platform.OS === 'android') {
  90. // file = path
  91. // } else {
  92. // file = path.replace('file://', '')
  93. // }
  94. // const file_list = file.split('/');
  95. // let fileArr = file.split('.');
  96. // let fileName = file_list[file_list.length - 1]
  97. // let mime = `.${fileArr[fileArr.length - 1]}`
  98. // if (mime.indexOf('mmexport') > -1) {
  99. // mime = '.jpg'
  100. // fileName = fileName.split('.')[0] + mime
  101. // }
  102. // this.props.photos.push(
  103. // {
  104. // img_id: '',
  105. // type: 'img',
  106. // mime: mime,
  107. // uri: file,
  108. // fileName: fileName
  109. // }
  110. // )
  111. // }
  112. // if (this.props.maxFiles) {
  113. // if (this.props.photos.length > this.props.maxFiles) {
  114. // Toast.info('仅能上传' + this.props.maxFiles + '张照片')
  115. // const photos = this.props.photos.slice(0, this.props.maxFiles)
  116. // this.props.onFileUpload(photos)
  117. // } else {
  118. // this.props.onFileUpload(this.props.photos)
  119. // }
  120. // }
  121. //
  122. // }).catch((e) => {
  123. // this.setState({visible: false,})
  124. // // console.log(11111111113,e)
  125. // // Toast.info('未选择文件或文件异常!',2)
  126. // });
  127. // }
  128. }
  129. const styles = StyleSheet.create({
  130. cameraBtn: {
  131. padding: 5
  132. },
  133. count: {
  134. color: '#fff',
  135. fontSize: 12
  136. },
  137. fullBtn: {
  138. justifyContent: 'center',
  139. alignItems: 'center',
  140. backgroundColor: '#fff'
  141. },
  142. countBox: {
  143. position: 'absolute',
  144. right: -5,
  145. top: -5,
  146. alignItems: 'center',
  147. backgroundColor: '#34A853',
  148. width: 16,
  149. height: 16,
  150. borderRadius: 8,
  151. justifyContent: 'center'
  152. }
  153. });
  154. export default MultiCameraButton;