File tree Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Expand file tree Collapse file tree 3 files changed +79
-0
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ yarn add @react-native-community/hooks
26
26
- [ useCameraRoll] ( https://github.com/react-native-community/hooks#usecameraroll )
27
27
- [ useClipboard] ( https://github.com/react-native-community/hooks#useclipboard )
28
28
- [ useDimensions] ( https://github.com/react-native-community/hooks#usedimensions )
29
+ - [ useImageDimensions] ( https://github.com/react-native-community/hooks#useImageDimensions )
29
30
- [ useKeyboard] ( https://github.com/react-native-community/hooks#usekeyboard )
30
31
- [ useInteractionManager] ( https://github.com/react-native-community/hooks#useinteractionmanager )
31
32
- [ useDeviceOrientation] ( https://github.com/react-native-community/hooks#usedeviceorientation )
@@ -104,6 +105,23 @@ const { width, height } = useDimensions().window
104
105
const screen = useDimensions ().screen
105
106
```
106
107
108
+ ### ` useImageDimensions `
109
+
110
+ ``` js
111
+ import {useImageDimensions } from ' @react-native-community/hooks'
112
+
113
+ const source = require (' ./assets/yourImage.png' )
114
+ // or
115
+ const source = {uri: ' https://your.image.URI' }
116
+
117
+ const {dimensions , loading , error } = useImageDimensions (source)
118
+
119
+ if (loading || error || ! dimensions) {
120
+ return null
121
+ }
122
+ const {width , height , aspectRatio } = dimensions
123
+ ```
124
+
107
125
### ` useKeyboard `
108
126
109
127
``` js
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import {useKeyboard} from './useKeyboard'
8
8
import { useInteractionManager } from './useInteractionManager'
9
9
import { useDeviceOrientation } from './useDeviceOrientation'
10
10
import { useLayout } from './useLayout'
11
+ import { useImageDimensions } from './useImageDimensions'
11
12
12
13
export {
13
14
useDimensions ,
@@ -20,4 +21,5 @@ export {
20
21
useInteractionManager ,
21
22
useDeviceOrientation ,
22
23
useLayout ,
24
+ useImageDimensions ,
23
25
}
Original file line number Diff line number Diff line change
1
+ import { useEffect , useState } from 'react'
2
+ import { Image , ImageRequireSource } from 'react-native'
3
+
4
+ export interface URISource {
5
+ uri : string
6
+ }
7
+
8
+ export type Source = ImageRequireSource | URISource
9
+
10
+ export interface ImageDimensions {
11
+ dimensions ?: { width : number ; height : number ; aspectRatio : number }
12
+ error ?: Error
13
+ loading : boolean
14
+ }
15
+
16
+ /**
17
+ * @param source either a remote URL or a local file resource.
18
+ * @returns original image dimensions (width, height and aspect ratio).
19
+ */
20
+ export function useImageDimensions ( source : Source ) : ImageDimensions {
21
+ const [ result , setResult ] = useState < ImageDimensions > ( { loading : true } )
22
+
23
+ useEffect ( ( ) => {
24
+ try {
25
+ if ( typeof source === 'number' ) {
26
+ const { width, height} = Image . resolveAssetSource ( source )
27
+
28
+ setResult ( {
29
+ dimensions : { width, height, aspectRatio : width / height } ,
30
+ loading : false ,
31
+ } )
32
+
33
+ return
34
+ }
35
+
36
+ if ( typeof source === 'object' && source . uri ) {
37
+ setResult ( { loading : true } )
38
+
39
+ Image . getSize (
40
+ source . uri ,
41
+ ( width , height ) =>
42
+ setResult ( {
43
+ dimensions : { width, height, aspectRatio : width / height } ,
44
+ loading : false ,
45
+ } ) ,
46
+ ( error ) => setResult ( { error, loading : false } ) ,
47
+ )
48
+
49
+ return
50
+ }
51
+
52
+ throw new Error ( 'not implemented' )
53
+ } catch ( error ) {
54
+ setResult ( { error, loading : false } )
55
+ }
56
+ } , [ source ] )
57
+
58
+ return result
59
+ }
You can’t perform that action at this time.
0 commit comments