ChosePhoto.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * Sample React Native App
  3. * https://github.com/facebook/react-native
  4. *
  5. * @format
  6. * @flow
  7. */
  8. import React, { Component } from 'react';
  9. import {
  10. Platform,
  11. StyleSheet,
  12. Text,
  13. View,
  14. Image,
  15. TouchableOpacity,
  16. ImageBackground,
  17. TextInput,
  18. Button,
  19. StatusBar,
  20. Modal,
  21. TouchableHighlight,
  22. DeviceEventEmitter
  23. } from 'react-native';
  24. import ImagePicker from 'react-native-image-picker';
  25. import ModalView from '../utils/ModalUtil';
  26. type Props = {};
  27. const options = {};
  28. export default class ChosePhoto extends Component<Props> {
  29. state = {
  30. modalVisible: false
  31. };
  32. setModalVisible(visible) {
  33. this.setState({
  34. modalVisible: visible
  35. });
  36. }
  37. render() {
  38. return (
  39. <ModalView
  40. close={() => {
  41. this.setState({ modalVisible: false });
  42. }}
  43. visible={this.state.modalVisible}
  44. customerlayout={{ flex: 1, justifyContent: 'flex-end' }}
  45. contentView={this.getView}
  46. />
  47. );
  48. }
  49. getView = () => {
  50. return (
  51. <Modal
  52. animationType="slide"
  53. transparent={true}
  54. visible={this.state.modalVisible}
  55. onRequestClose={() => {
  56. this.setState({ modalVisible: false });
  57. }}
  58. >
  59. <View
  60. style={{
  61. flex: 1,
  62. flexDirection: 'column'
  63. }}
  64. >
  65. <TouchableOpacity
  66. style={{
  67. flex: 2.5,
  68. width: '100%'
  69. }}
  70. activeOpacity={1}
  71. onPress={() => this.setState({ modalVisible: false })}
  72. >
  73. <View
  74. style={{
  75. flex: 2,
  76. width: '100%'
  77. }}
  78. />
  79. </TouchableOpacity>
  80. <View
  81. style={{
  82. flex: 1,
  83. width: '100%',
  84. flexDirection: 'column',
  85. alignItems: 'center',
  86. justifyContent: 'center'
  87. }}
  88. >
  89. <View
  90. style={{
  91. flex: 2,
  92. width: '90%',
  93. backgroundColor: 'white',
  94. borderRadius: 20,
  95. flexDirection: 'column'
  96. }}
  97. >
  98. <TouchableOpacity
  99. style={{
  100. flex: 5,
  101. alignItems: 'center',
  102. justifyContent: 'center'
  103. }}
  104. activeOpacity={1}
  105. onPress={this.photograph.bind(this)}
  106. >
  107. <View
  108. style={{
  109. flex: 1,
  110. alignItems: 'center',
  111. justifyContent: 'center'
  112. }}
  113. >
  114. <Text style={{ fontSize: 22, color: 'rgba(88, 168, 250, 1)' }}>拍照</Text>
  115. </View>
  116. </TouchableOpacity>
  117. <View
  118. style={{
  119. flex: 0.1,
  120. backgroundColor: 'rgba(246, 247, 248, 1)'
  121. }}
  122. />
  123. <TouchableOpacity
  124. style={{
  125. flex: 5,
  126. alignItems: 'center',
  127. justifyContent: 'center'
  128. }}
  129. activeOpacity={1}
  130. onPress={this.album_selection.bind(this)}
  131. >
  132. <View
  133. style={{
  134. flex: 1,
  135. alignItems: 'center',
  136. justifyContent: 'center'
  137. }}
  138. >
  139. <Text style={{ fontSize: 22, color: 'rgba(88, 168, 250, 1)' }}>从相册选择</Text>
  140. </View>
  141. </TouchableOpacity>
  142. </View>
  143. <View
  144. style={{
  145. flex: 0.1,
  146. width: '100%'
  147. }}
  148. />
  149. <TouchableOpacity
  150. style={{
  151. flex: 1,
  152. width: '90%',
  153. alignItems: 'center',
  154. justifyContent: 'center',
  155. borderRadius: 20,
  156. backgroundColor: 'white'
  157. }}
  158. activeOpacity={1}
  159. onPress={() => {
  160. this.setState({ modalVisible: false });
  161. }}
  162. >
  163. <View
  164. style={{
  165. flex: 1,
  166. alignItems: 'center',
  167. justifyContent: 'center',
  168. borderRadius: 20,
  169. backgroundColor: 'white'
  170. }}
  171. >
  172. <Text
  173. style={{
  174. // fontWeight: "bold",
  175. fontSize: 22,
  176. color: 'rgba(88, 168, 250, 1)'
  177. }}
  178. >
  179. 取消
  180. </Text>
  181. </View>
  182. </TouchableOpacity>
  183. <View
  184. style={{
  185. flex: 0.1,
  186. width: '100%'
  187. }}
  188. />
  189. </View>
  190. </View>
  191. </Modal>
  192. );
  193. };
  194. photograph() {
  195. this.setModalVisible(false);
  196. //拍照
  197. ImagePicker.launchCamera(options, (response) => {
  198. if (response.error) {
  199. alert('ImagePicker Error: ' + response.error);
  200. }
  201. this.props.photoback(response.uri);
  202. });
  203. }
  204. album_selection() {
  205. this.setModalVisible(false);
  206. //打开系统相册
  207. ImagePicker.launchImageLibrary(options, (response) => {
  208. if (response.error) {
  209. alert('ImagePicker Error: ' + response.error);
  210. }
  211. this.props.photoback(response.uri);
  212. });
  213. }
  214. }