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
2626- [ useCameraRoll] ( https://github.com/react-native-community/hooks#usecameraroll )
2727- [ useClipboard] ( https://github.com/react-native-community/hooks#useclipboard )
2828- [ useDimensions] ( https://github.com/react-native-community/hooks#usedimensions )
29+ - [ useImageDimensions] ( https://github.com/react-native-community/hooks#useImageDimensions )
2930- [ useKeyboard] ( https://github.com/react-native-community/hooks#usekeyboard )
3031- [ useInteractionManager] ( https://github.com/react-native-community/hooks#useinteractionmanager )
3132- [ useDeviceOrientation] ( https://github.com/react-native-community/hooks#usedeviceorientation )
@@ -104,6 +105,23 @@ const { width, height } = useDimensions().window
104105const screen = useDimensions ().screen
105106```
106107
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+
107125### ` useKeyboard `
108126
109127``` js
Original file line number Diff line number Diff line change @@ -8,6 +8,7 @@ import {useKeyboard} from './useKeyboard'
88import { useInteractionManager } from './useInteractionManager'
99import { useDeviceOrientation } from './useDeviceOrientation'
1010import { useLayout } from './useLayout'
11+ import { useImageDimensions } from './useImageDimensions'
1112
1213export {
1314 useDimensions ,
@@ -20,4 +21,5 @@ export {
2021 useInteractionManager ,
2122 useDeviceOrientation ,
2223 useLayout ,
24+ useImageDimensions ,
2325}
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