11import { useEffect , useState } from 'react'
2- import { Image , ImageRequireSource } from 'react-native'
2+ import { Image , ImageRequireSource , ImageURISource } from 'react-native'
33
44export interface URISource {
55 uri : string
@@ -21,10 +21,12 @@ export interface ImageDimensionsResult {
2121
2222/**
2323 * @param source either a remote URL or a local file resource.
24+ * @param headers headers to be passed to a remote URL resource.
2425 * @returns original image dimensions (width, height and aspect ratio).
2526 */
2627export function useImageDimensions (
2728 source : ImageDimensionsSource ,
29+ headers ?: ImageURISource [ 'headers' ] ,
2830) : ImageDimensionsResult {
2931 const [ result , setResult ] = useState < ImageDimensionsResult > ( { loading : true } )
3032
@@ -44,15 +46,28 @@ export function useImageDimensions(
4446 if ( typeof source === 'object' && source . uri ) {
4547 setResult ( { loading : true } )
4648
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- )
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+ }
5671
5772 return
5873 }
@@ -61,7 +76,7 @@ export function useImageDimensions(
6176 } catch ( error ) {
6277 setResult ( { error, loading : false } )
6378 }
64- } , [ source ] )
79+ } , [ source , headers ] )
6580
6681 return result
6782}
0 commit comments