1
1
import { useEffect , useState } from 'react'
2
- import { Image , ImageRequireSource } from 'react-native'
2
+ import { Image , ImageRequireSource , ImageURISource } from 'react-native'
3
3
4
4
export interface URISource {
5
5
uri : string
@@ -21,10 +21,12 @@ export interface ImageDimensionsResult {
21
21
22
22
/**
23
23
* @param source either a remote URL or a local file resource.
24
+ * @param headers headers to be passed to a remote URL resource.
24
25
* @returns original image dimensions (width, height and aspect ratio).
25
26
*/
26
27
export function useImageDimensions (
27
28
source : ImageDimensionsSource ,
29
+ headers ?: ImageURISource [ 'headers' ]
28
30
) : ImageDimensionsResult {
29
31
const [ result , setResult ] = useState < ImageDimensionsResult > ( { loading : true } )
30
32
@@ -43,16 +45,29 @@ export function useImageDimensions(
43
45
44
46
if ( typeof source === 'object' && source . uri ) {
45
47
setResult ( { loading : true } )
46
-
47
- Image . getSize (
48
- source . uri ,
49
- ( width , height ) =>
50
- setResult ( {
51
- dimensions : { width, height, aspectRatio : width / height } ,
52
- loading : false ,
53
- } ) ,
54
- ( error ) => setResult ( { error, loading : false } ) ,
55
- )
48
+
49
+ if ( typeof headers === 'object' ) {
50
+ Image . getSizeWithHeaders (
51
+ source . uri ,
52
+ headers ,
53
+ ( width , height ) =>
54
+ setResult ( {
55
+ dimensions : { width, height, aspectRatio : width / height } ,
56
+ loading : false ,
57
+ } ) ,
58
+ ( error ) => setResult ( { error, loading : false } ) ,
59
+ )
60
+ } else {
61
+ Image . getSize (
62
+ source . uri ,
63
+ ( width , height ) =>
64
+ setResult ( {
65
+ dimensions : { width, height, aspectRatio : width / height } ,
66
+ loading : false ,
67
+ } ) ,
68
+ ( error ) => setResult ( { error, loading : false } ) ,
69
+ )
70
+ }
56
71
57
72
return
58
73
}
0 commit comments