BrowseImage.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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 = {};
  24. class Row extends Component {
  25. constructor(props) {
  26. super(props);
  27. this.state = {
  28. visible: false,
  29. };
  30. }
  31. setVisible = (visible) => {
  32. // this.props.visible = visible
  33. this.setState({visible: visible});
  34. };
  35. calcSize = (item) => {
  36. let width, height = 0;
  37. if (item.width < SCREEN_WIDTH) {
  38. width = item.width;
  39. } else {
  40. width = SCREEN_WIDTH - 20;
  41. }
  42. height = Math.floor((width * item.height) / item.width);
  43. return {marginHorizontal: 5, marginVertical: 5, width, height};
  44. };
  45. render() {
  46. const {row, id} = this.props;
  47. const baseURL = SyncStorage.get('baseURL');
  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. const baseURL = SyncStorage.get('baseURL');
  90. subjectImages.push({url: baseURL+item.picture});
  91. return (
  92. <TouchableOpacity onPress={() => this._zoomImage(index, 'S')}>
  93. <Row
  94. ref={el => {
  95. let key = String(index);
  96. _subjectRows[key] = el;
  97. }}
  98. id={index}
  99. row={item}
  100. />
  101. </TouchableOpacity>
  102. );
  103. }
  104. onViewableSubjectsChanged(info) {
  105. info.changed.forEach((currentValue, index, arr) => {
  106. _subjectRows[currentValue.key].setVisible(currentValue.isViewable);
  107. });
  108. }
  109. _imgViewRender = () => {
  110. return (
  111. <View style={{
  112. flexDirection: 'row',
  113. justifyContent: 'flex-end',
  114. margin: 20,
  115. }}>
  116. <Text style={{color: '#ff6633', marginRight: 10}}>长按保存图片</Text>
  117. <TouchableOpacity style={{
  118. borderRadius: 20,
  119. borderWidth: 1,
  120. borderColor: '#ff6633',
  121. }} onPress={() => this.setState({answerVisible: false, subjectVisible: false})}>
  122. <Text style={{
  123. fontFamily: 'iconfont',
  124. color: '#ff6633',
  125. fontSize: 20,
  126. textAlign: 'center',
  127. }}>{'\ue606'}</Text>
  128. </TouchableOpacity>
  129. </View>
  130. );
  131. };
  132. _saveImgRender = (cancel, saveToLocal) => {
  133. return (
  134. <View style={{
  135. flexDirection: 'row',
  136. justifyContent: 'center',
  137. margin: 20,
  138. marginTop: SCREEN_HEIGHT - 200,
  139. }}>
  140. <TouchableOpacity style={{
  141. borderRadius: 20,
  142. backgroundColor: '#eee',
  143. paddingHorizontal: 10,
  144. paddingVertical: 5,
  145. borderColor: '#ff6633',
  146. borderWidth: 1,
  147. marginRight: 10,
  148. }} onPress={() => cancel()}>
  149. <Text style={{
  150. fontFamily: 'iconfont',
  151. color: '#ff6633',
  152. fontSize: 20,
  153. textAlign: 'center',
  154. }}>取消</Text>
  155. </TouchableOpacity>
  156. <TouchableOpacity style={{
  157. borderRadius: 20,
  158. backgroundColor: '#eee',
  159. paddingHorizontal: 10,
  160. paddingVertical: 5,
  161. borderColor: '#ff6633',
  162. borderWidth: 1,
  163. marginRight: 10,
  164. }} onPress={() => saveToLocal()}>
  165. <Text style={{
  166. fontFamily: 'iconfont',
  167. color: '#ff6633',
  168. fontSize: 20,
  169. textAlign: 'center',
  170. }}>保存</Text>
  171. </TouchableOpacity>
  172. </View>
  173. );
  174. };
  175. savePhoto = (url) => {
  176. const imgFile = url.split('/');
  177. const imgDIR = `${RNFS.DocumentDirectoryPath}/${imgFile[imgFile.length - 1]}`;
  178. const ret = RNFS.downloadFile(
  179. {
  180. fromUrl: url,
  181. toFile: imgDIR,
  182. });
  183. ret.promise.then(res => {
  184. if (res && res.statusCode === 200) {
  185. let promise = CameraRoll.saveToCameraRoll('file://' + imgDIR, 'photo');
  186. promise.then(function (result) {
  187. ToastAndroid.show('已保存到相册', ToastAndroid.SHORT);
  188. // Alert.alert('友情提醒', '已保存到相册!',
  189. // [
  190. // {text: '我知道了', style: 'cancel'},
  191. // ]);
  192. }).catch(function (error) {
  193. console.log(4444444444, error);
  194. // alert('保存失败!\n' + error);
  195. Alert.alert('友情提醒', '保存失败!',
  196. [
  197. {text: '我知道了', style: 'cancel'},
  198. ]);
  199. });
  200. }
  201. });
  202. };
  203. render() {
  204. const {processImageData, loading} = this.props.customer;
  205. subjectImages = [];
  206. let subjectsRender = '';
  207. if (loading) {
  208. subjectsRender = <Text style={styles.uploadText}>
  209. 努力加载中...
  210. </Text>;
  211. } else {
  212. subjectsRender = <FlatList
  213. data={processImageData}
  214. renderItem={({item, index}) => this.renderSubjectItem(item, index)}
  215. onViewableItemsChanged={this.onViewableSubjectsChanged}
  216. keyExtractor={this._keyExtractorSubject}
  217. />;
  218. }
  219. return (
  220. <Provider>
  221. {loading ?
  222. <ActivityIndicator
  223. animating={true}
  224. toast
  225. size="large"
  226. text="加载中..."
  227. /> :
  228. <View key="subject" backgroundColor='#fff'>
  229. {subjectsRender}
  230. </View>
  231. }
  232. <Modal
  233. visible={this.state.subjectVisible}
  234. transparent={true}
  235. animationType={'fade'}
  236. onRequestClose={() => this.setState({subjectVisible: false})}>
  237. <ImageViewer
  238. style={{menuContainer: -20}}
  239. imageUrls={subjectImages}
  240. index={this.state.imageIndex}
  241. renderHeader={() => this._imgViewRender()}
  242. saveToLocalByLongPress={true}
  243. menus={({cancel, saveToLocal}) => this._saveImgRender(cancel, saveToLocal)}
  244. onSave={(url) => this.savePhoto(url)}
  245. />
  246. </Modal>
  247. </Provider>
  248. );
  249. }
  250. }
  251. export const styles = StyleSheet.create({
  252. uploadStyle: {
  253. justifyContent: 'center',
  254. padding: 10,
  255. marginVertical: 10,
  256. },
  257. uploadText: {
  258. textAlign: 'center',
  259. padding: 3,
  260. paddingTop: 10,
  261. borderRadius: 5,
  262. height: 35,
  263. marginLeft: 2,
  264. borderWidth: 1,
  265. borderColor: '#ddd',
  266. overflow: 'hidden',
  267. fontFamily: 'iconfont',
  268. fontSize: 14,
  269. color: '#333',
  270. },
  271. });
  272. export default BrowseImage;