import React, {Component} from 'react'; import { View, Text, TouchableOpacity, StyleSheet, Dimensions, FlatList, Modal, Alert, ToastAndroid, } from 'react-native'; import CameraRoll from '@react-native-community/cameraroll'; import {createAction} from '../../utils'; import {connect} from 'react-redux'; import {ActivityIndicator, Provider} from '@ant-design/react-native'; import {CachedImage} from 'react-native-cached-images-zzly'; import ImageViewer from 'react-native-image-zoom-viewer'; import RNFS from 'react-native-fs'; import SyncStorage from 'sync-storage'; const SCREEN_WIDTH = Dimensions.get('window').width; const SCREEN_HEIGHT = Dimensions.get('window').height; //const IMAGE_PLACEHOLDER = require('../../../assets/images/bg_mine_header.png'); let subjectImages = []; let _subjectRows = {}, _answerRows = {}; const baseURL = SyncStorage.get('baseURL'); class Row extends Component { constructor(props) { super(props); this.state = { visible: false, }; } setVisible = (visible) => { // this.props.visible = visible this.setState({visible: visible}); }; calcSize = (item) => { let width, height = 0; if (item.width < SCREEN_WIDTH) { width = item.width; } else { width = SCREEN_WIDTH - 20; } height = Math.floor((width * item.height) / item.width); return {marginHorizontal: 5, marginVertical: 5, width, height}; }; render() { const {row, id} = this.props; const style = this.calcSize(row); const height = Math.floor((SCREEN_WIDTH * row.height) / row.width) + 20; const url = baseURL + row.picture //console.log('id=' + id, 'visible=' + this.state.visible) // if(!this.state.visible){ // //取消网络图片的加载 // ImageCache.get().cancel(row.uri); // } return ( { this.state.visible && } ); } } @connect(customer => ({...customer})) class BrowseImage extends Component { // 进度浏览图片 constructor(props) { super(props); this.state = { id: this.props.navigation.state.params.id, subjectVisible: false, saveImgVisible: false, saveToLocal: '', imageIndex: 0, }; } _keyExtractorSubject = (item, index) => String(index); componentDidMount() { this._fetchData(); } _fetchData = () => { this.props.dispatch(createAction('customer/fetchProcessImage')({id: this.state.id})); }; _zoomImage = (id, type) => { this.setState({subjectVisible: true, imageIndex: id}); }; renderSubjectItem(item, index) { subjectImages.push({url: baseURL+item.picture}); return ( this._zoomImage(index, 'S')}> { let key = String(index); _subjectRows[key] = el; }} id={index} row={item} /> ); } onViewableSubjectsChanged(info) { info.changed.forEach((currentValue, index, arr) => { _subjectRows[currentValue.key].setVisible(currentValue.isViewable); }); } _imgViewRender = () => { return ( 长按保存图片 this.setState({answerVisible: false, subjectVisible: false})}> {'\ue606'} ); }; _saveImgRender = (cancel, saveToLocal) => { return ( cancel()}> 取消 saveToLocal()}> 保存 ); }; savePhoto = (url) => { const imgFile = url.split('/'); const imgDIR = `${RNFS.DocumentDirectoryPath}/${imgFile[imgFile.length - 1]}`; const ret = RNFS.downloadFile( { fromUrl: url, toFile: imgDIR, }); ret.promise.then(res => { if (res && res.statusCode === 200) { let promise = CameraRoll.saveToCameraRoll('file://' + imgDIR, 'photo'); promise.then(function (result) { ToastAndroid.show('已保存到相册', ToastAndroid.SHORT); // Alert.alert('友情提醒', '已保存到相册!', // [ // {text: '我知道了', style: 'cancel'}, // ]); }).catch(function (error) { console.log(4444444444, error); // alert('保存失败!\n' + error); Alert.alert('友情提醒', '保存失败!', [ {text: '我知道了', style: 'cancel'}, ]); }); } }); }; render() { const {processImageData, loading} = this.props.customer; subjectImages = []; let subjectsRender = ''; if (loading) { subjectsRender = 努力加载中... ; } else { subjectsRender = this.renderSubjectItem(item, index)} onViewableItemsChanged={this.onViewableSubjectsChanged} keyExtractor={this._keyExtractorSubject} />; } return ( {loading ? : {subjectsRender} } this.setState({subjectVisible: false})}> this._imgViewRender()} saveToLocalByLongPress={true} menus={({cancel, saveToLocal}) => this._saveImgRender(cancel, saveToLocal)} onSave={(url) => this.savePhoto(url)} /> ); } } export const styles = StyleSheet.create({ uploadStyle: { justifyContent: 'center', padding: 10, marginVertical: 10, }, uploadText: { textAlign: 'center', padding: 3, paddingTop: 10, borderRadius: 5, height: 35, marginLeft: 2, borderWidth: 1, borderColor: '#ddd', overflow: 'hidden', fontFamily: 'iconfont', fontSize: 14, color: '#333', }, }); export default BrowseImage;