Repository navigation
Expand file tree
/
Copy pathImageViewer.js
More file actions
124 lines (113 loc) · 3.26 KB
/
Copy pathImageViewer.js
File metadata and controls
124 lines (113 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* 图片预览界面
*/
import * as React from 'react'
import { Dimensions, Image, TouchableOpacity, Platform } from 'react-native'
import ImageZoom from 'react-native-image-pan-zoom'
// eslint-disable-next-line import/no-unresolved
import PhotoView from 'react-native-photo-view'
import styles from './styles'
export default class ImageViewer extends React.Component {
props: {
uri: Object,
containerStyle: any,
backAction: () => {},
}
static defaultProps = {
containerStyle: styles.container,
}
shouldComponentUpdate(nextProps, nextState) {
return (
JSON.stringify(this.props) !== JSON.stringify(nextProps) ||
JSON.stringify(this.state) !== JSON.stringify(nextState)
)
}
handleLayout = event => {
let imageZoomShouldUpdate = false
let imageShouldUpdate = false
if (event.nativeEvent.layout.width !== this.width) {
this.width = event.nativeEvent.layout.width
this.height = event.nativeEvent.layout.height
imageZoomShouldUpdate = true
}
Image.getSize(this.props.uri, (width, height) => {
this.imageWidth = 200
this.imageHeight = 200
if (this.imageWidth !== width || this.imageHeight !== height) {
imageShouldUpdate = true
this.imageWidth = width
this.imageHeight = height
if (width > this.width || height > this.height) {
let imgScale = this.imageHeight / this.imageWidth
let imgViewScale = this.height / this.width
if (imgScale > imgViewScale) {
this.imageHeight = this.height
this.imageWidth = this.height / imgScale
} else {
this.imageWidth = this.width
this.imageHeight = this.width * imgScale
}
}
}
// 强制刷新
(imageZoomShouldUpdate || imageShouldUpdate) && this.forceUpdate()
})
// this.forceUpdate()
}
renderIOS = () => {
return (
<ImageZoom
onClick={() => {
if (typeof this.props.backAction === 'function') {
this.props.backAction()
}
}}
cropWidth={Dimensions.get('window').width}
cropHeight={Dimensions.get('window').height}
imageWidth={this.imageWidth}
imageHeight={this.imageHeight}
// enableCenterFocus={true}
>
<Image
style={{ width: this.imageWidth, height: this.imageHeight }}
source={{ uri: this.props.uri }}
/>
</ImageZoom>
)
}
renderAndroid = () => {
return (
<PhotoView
source={{ uri: this.props.uri }}
minimumZoomScale={0.5}
maximumZoomScale={3}
androidScaleType="center"
style={{
// width: this.imageWidth,
// height: this.imageHeight,
top: 0,
left: 0,
right: 0,
bottom: 0,
position: 'absolute',
}}
/>
)
}
render() {
return (
<TouchableOpacity
onLayout={this.handleLayout}
activeOpacity={1}
style={this.props.containerStyle}
onPress={() => {
if (typeof this.props.backAction === 'function') {
this.props.backAction()
}
}}
>
{Platform.OS === 'ios' ? this.renderIOS() : this.renderAndroid()}
</TouchableOpacity>
)
}
}