@@ -19,6 +19,7 @@ class ImageView extends PureComponent {
19
19
this . scrollableRef = null ;
20
20
this . canUseTouchScreen = canUseTouchScreen ( ) ;
21
21
this . state = {
22
+ containerHeight : 0 ,
22
23
isZoomed : false ,
23
24
isDragging : false ,
24
25
isMouseDown : false ,
@@ -41,8 +42,7 @@ class ImageView extends PureComponent {
41
42
return ;
42
43
}
43
44
Image . getSize ( this . props . url , ( width , height ) => {
44
- const scale = Math . max ( this . props . windowWidth / width , this . props . windowHeight / height ) ;
45
- this . setImageRegion ( width , height , scale ) ;
45
+ this . setImageRegion ( width , height ) ;
46
46
} ) ;
47
47
document . addEventListener ( 'mousemove' , this . trackMovement . bind ( this ) ) ;
48
48
}
@@ -59,9 +59,8 @@ class ImageView extends PureComponent {
59
59
* When open image, set image left/right/top/bottom point and width, height
60
60
* @param {Boolean } width image width
61
61
* @param {Boolean } height image height
62
- * @param {Boolean } scale zoomscale when click zoom
63
62
*/
64
- setImageRegion ( width , height , scale ) {
63
+ setImageRegion ( width , height ) {
65
64
let imgLeft = ( this . props . windowWidth - width ) / 2 ;
66
65
let imgRight = ( ( this . props . windowWidth - width ) / 2 ) + width ;
67
66
let imgTop = ( this . props . windowHeight - height ) / 2 ;
@@ -86,7 +85,7 @@ class ImageView extends PureComponent {
86
85
}
87
86
88
87
this . setState ( {
89
- imgWidth : width , imgHeight : height , zoomScale : scale , imageLeft : imgLeft , imageTop : imgTop , imageRight : imgRight , imageBottom : imgBottom ,
88
+ imgWidth : width , imgHeight : height , imageLeft : imgLeft , imageTop : imgTop , imageRight : imgRight , imageBottom : imgBottom ,
90
89
} ) ;
91
90
}
92
91
@@ -162,19 +161,30 @@ class ImageView extends PureComponent {
162
161
return (
163
162
< View
164
163
ref = { el => this . scrollableRef = el }
164
+ onLayout = { ( e ) => {
165
+ const { width, height} = e . nativeEvent . layout ;
166
+ const imageWidth = this . state . imgWidth ;
167
+ const imageHeight = this . state . imgHeight ;
168
+ const scale = imageHeight && imageWidth ? Math . min ( width / imageWidth , height / imageHeight ) : 0 ;
169
+ this . setState ( {
170
+ containerHeight : height ,
171
+ zoomScale : scale ,
172
+ } ) ;
173
+ } }
165
174
style = { [
166
175
styles . imageViewContainer ,
167
176
styles . overflowScroll ,
168
177
styles . noScrollbars ,
178
+ styles . pRelative ,
169
179
] }
170
180
>
171
181
< Pressable
172
- style = { [
173
- styles . w100 ,
174
- styles . h100 ,
175
- styles . flex1 ,
176
- getZoomCursorStyle ( this . state . isZoomed , this . state . isDragging ) ,
177
- ] }
182
+ style = { {
183
+ ... getZoomSizingStyle ( this . state . isZoomed , this . state . imgWidth , this . state . imgHeight , this . state . zoomScale , this . state . containerHeight ) ,
184
+ ... getZoomCursorStyle ( this . state . isZoomed , this . state . isDragging ) ,
185
+ ... this . state . isZoomed ? styles . pRelative : styles . pAbsolute ,
186
+ ... styles . flex1 ,
187
+ } }
178
188
onPressIn = { ( e ) => {
179
189
const { pageX, pageY} = e . nativeEvent ;
180
190
this . setState ( {
@@ -212,7 +222,10 @@ class ImageView extends PureComponent {
212
222
>
213
223
< Image
214
224
source = { { uri : this . props . url } }
215
- style = { getZoomSizingStyle ( this . state . isZoomed , this . state . imgWidth , this . state . imgHeight , this . state . zoomScale ) }
225
+ style = { [
226
+ styles . h100 ,
227
+ styles . w100 ,
228
+ ] }
216
229
resizeMode = { this . state . isZoomed ? 'contain' : 'center' }
217
230
/>
218
231
</ Pressable >
0 commit comments