BrowseImage.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import React, {Component} from 'react';
  2. import {
  3. View,
  4. Text,
  5. TouchableOpacity,
  6. StyleSheet,
  7. Dimensions,
  8. FlatList,
  9. Modal, Alert, ToastAndroid,
  10. } from 'react-native';
  11. import CameraRoll from '@react-native-community/cameraroll';
  12. import {createAction} from '../../utils';
  13. import {connect} from 'react-redux';
  14. import {ActivityIndicator, Provider} from '@ant-design/react-native';
  15. import {CachedImage} from 'react-native-cached-images-zzly';
  16. import ImageViewer from 'react-native-image-zoom-viewer';
  17. import RNFS from 'react-native-fs';
  18. import SyncStorage from 'sync-storage';
  19. const SCREEN_WIDTH = Dimensions.get('window').width;
  20. const SCREEN_HEIGHT = Dimensions.get('window').height;
  21. //const IMAGE_PLACEHOLDER = require('../../../assets/images/bg_mine_header.png');
  22. let subjectImages = [];
  23. let _subjectRows = {}, _answerRows = {};
  24. const baseURL = SyncStorage.get('baseURL');
  25. class Row extends Component {
  26. constructor(props) {
  27. super(props);
  28. this.state = {
  29. visible: false,
  30. };
  31. }
  32. setVisible = (visible) => {
  33. // this.props.visible = visible
  34. this.setState({visible: visible});
  35. };
  36. calcSize = (item) => {
  37. let width, height = 0;
  38. if (item.width < SCREEN_WIDTH) {
  39. width = item.width;
  40. } else {
  41. width = SCREEN_WIDTH - 20;
  42. }
  43. height = Math.floor((width * item.height) / item.width);
  44. return {marginHorizontal: 5, marginVertical: 5, width, height};
  45. };
  46. render() {
  47. const {row, id} = this.props;
  48. const style = this.calcSize(row);
  49. const height = Math.floor((SCREEN_WIDTH * row.height) / row.width) + 20;
  50. const url = baseURL + row.picture
  51. //console.log('id=' + id, 'visible=' + this.state.visible)
  52. // if(!this.state.visible){
  53. // //取消网络图片的加载
  54. // ImageCache.get().cancel(row.uri);
  55. // }
  56. return (
  57. <View style={{borderBottomWidth: 10, borderBottomColor: '#f5f5f5', height}}>
  58. {
  59. this.state.visible && <CachedImage source={{uri: url}} style={style}/>
  60. }
  61. </View>
  62. );
  63. }
  64. }
  65. @connect(customer => ({...customer}))
  66. class BrowseImage extends Component {
  67. // 进度浏览图片
  68. constructor(props) {
  69. super(props);
  70. this.state = {
  71. id: this.props.navigation.state.params.id,
  72. subjectVisible: false,
  73. saveImgVisible: false,
  74. saveToLocal: '',
  75. imageIndex: 0,
  76. };
  77. }
  78. _keyExtractorSubject = (item, index) => String(index);
  79. componentDidMount() {
  80. this._fetchData();
  81. }
  82. _fetchData = () => {
  83. this.props.dispatch(createAction('customer/fetchProcessImage')({id: this.state.id}));
  84. };
  85. _zoomImage = (id, type) => {
  86. this.setState({subjectVisible: true, imageIndex: id});
  87. };
  88. renderSubjectItem(item, index) {
  89. subjectImages.push({url: baseURL+item.picture});
  90. return (
  91. <TouchableOpacity onPress={() => this._zoomImage(index, 'S')}>
  92. <Row
  93. ref={el => {
  94. let key = String(index);
  95. _subjectRows[key] = el;
  96. }}
  97. id={index}
  98. row={item}
  99. />
  100. </TouchableOpacity>
  101. );
  102. }
  103. onViewableSubjectsChanged(info) {
  104. info.changed.forEach((currentValue, index, arr) => {
  105. _subjectRows[currentValue.key].setVisible(currentValue.isViewable);
  106. });
  107. }
  108. _imgViewRender = () => {
  109. return (
  110. <View style={{
  111. flexDirection: 'row',
  112. justifyContent: 'flex-end',
  113. margin: 20,
  114. }}>
  115. <Text style={{color: '#ff6633', marginRight: 10}}>长按保存图片</Text>
  116. <TouchableOpacity style={{
  117. borderRadius: 20,
  118. borderWidth: 1,
  119. borderColor: '#ff6633',
  120. }} onPress={() => this.setState({answerVisible: false, subjectVisible: false})}>
  121. <Text style={{
  122. fontFamily: 'iconfont',
  123. color: '#ff6633',
  124. fontSize: 20,
  125. textAlign: 'center',
  126. }}>{'\ue606'}</Text>
  127. </TouchableOpacity>
  128. </View>
  129. );
  130. };
  131. _saveImgRender = (cancel, saveToLocal) => {
  132. return (
  133. <View style={{
  134. flexDirection: 'row',
  135. justifyContent: 'center',
  136. margin: 20,
  137. marginTop: SCREEN_HEIGHT - 200,
  138. }}>
  139. <TouchableOpacity style={{
  140. borderRadius: 20,
  141. backgroundColor: '#eee',
  142. paddingHorizontal: 10,
  143. paddingVertical: 5,
  144. borderColor: '#ff6633',
  145. borderWidth: 1,
  146. marginRight: 10,
  147. }} onPress={() => cancel()}>
  148. <Text style={{
  149. fontFamily: 'iconfont',
  150. color: '#ff6633',
  151. fontSize: 20,
  152. textAlign: 'center',
  153. }}>取消</Text>
  154. </TouchableOpacity>
  155. <TouchableOpacity style={{
  156. borderRadius: 20,
  157. backgroundColor: '#eee',
  158. paddingHorizontal: 10,
  159. paddingVertical: 5,
  160. borderColor: '#ff6633',
  161. borderWidth: 1,
  162. marginRight: 10,
  163. }} onPress={() => saveToLocal()}>
  164. <Text style={{
  165. fontFamily: 'iconfont',
  166. color: '#ff6633',
  167. fontSize: 20,
  168. textAlign: 'center',
  169. }}>保存</Text>
  170. </TouchableOpacity>
  171. </View>
  172. );
  173. };
  174. savePhoto = (url) => {
  175. const imgFile = url.split('/');
  176. const imgDIR = `${RNFS.DocumentDirectoryPath}/${imgFile[imgFile.length - 1]}`;
  177. const ret = RNFS.downloadFile(
  178. {
  179. fromUrl: url,
  180. toFile: imgDIR,
  181. });
  182. ret.promise.then(res => {
  183. if (res && res.statusCode === 200) {
  184. let promise = CameraRoll.saveToCameraRoll('file://' + imgDIR, 'photo');
  185. promise.then(function (result) {
  186. ToastAndroid.show('已保存到相册', ToastAndroid.SHORT);
  187. // Alert.alert('友情提醒', '已保存到相册!',
  188. // [
  189. // {text: '我知道了', style: 'cancel'},
  190. // ]);
  191. }).catch(function (error) {
  192. console.log(4444444444, error);
  193. // alert('保存失败!\n' + error);
  194. Alert.alert('友情提醒', '保存失败!',
  195. [
  196. {text: '我知道了', style: 'cancel'},
  197. ]);
  198. });
  199. }
  200. });
  201. };
  202. render() {
  203. const {processImageData, loading} = this.props.customer;
  204. subjectImages = [];
  205. let subjectsRender = '';
  206. if (loading) {
  207. subjectsRender = <Text style={styles.uploadText}>
  208. 努力加载中...
  209. </Text>;
  210. } else {
  211. subjectsRender = <FlatList
  212. data={processImageData}
  213. renderItem={({item, index}) => this.renderSubjectItem(item, index)}
  214. onViewableItemsChanged={this.onViewableSubjectsChanged}
  215. keyExtractor={this._keyExtractorSubject}
  216. />;
  217. }
  218. return (
  219. <Provider>
  220. {loading ?
  221. <ActivityIndicator
  222. animating={true}
  223. toast
  224. size="large"
  225. text="加载中..."
  226. /> :
  227. <View key="subject" backgroundColor='#fff'>
  228. {subjectsRender}
  229. </View>
  230. }
  231. <Modal
  232. visible={this.state.subjectVisible}
  233. transparent={true}
  234. animationType={'fade'}
  235. onRequestClose={() => this.setState({subjectVisible: false})}>
  236. <ImageViewer
  237. style={{menuContainer: -20}}
  238. imageUrls={subjectImages}
  239. index={this.state.imageIndex}
  240. renderHeader={() => this._imgViewRender()}
  241. saveToLocalByLongPress={true}
  242. menus={({cancel, saveToLocal}) => this._saveImgRender(cancel, saveToLocal)}
  243. onSave={(url) => this.savePhoto(url)}
  244. />
  245. </Modal>
  246. </Provider>
  247. );
  248. }
  249. }
  250. export const styles = StyleSheet.create({
  251. uploadStyle: {
  252. justifyContent: 'center',
  253. padding: 10,
  254. marginVertical: 10,
  255. },
  256. uploadText: {
  257. textAlign: 'center',
  258. padding: 3,
  259. paddingTop: 10,
  260. borderRadius: 5,
  261. height: 35,
  262. marginLeft: 2,
  263. borderWidth: 1,
  264. borderColor: '#ddd',
  265. overflow: 'hidden',
  266. fontFamily: 'iconfont',
  267. fontSize: 14,
  268. color: '#333',
  269. },
  270. });
  271. export default BrowseImage;