-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
34 lines (31 loc) · 1.08 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import { Image } from 'react-native'
import React, { useEffect, useState } from 'react'
import { Image as ExpoImage } from 'expo-image'
const AutoHeightImage = ({ source, width, style }) => {
const [imgHeight, setImgHight] = useState(0)
useEffect(() => {
if (typeof source === 'number') {
const imgSize = Image.resolveAssetSource(source);
const aspectRatio = imgSize.width / imgSize.height;
const calculatedHeight = width / aspectRatio;
setImgHight(calculatedHeight)
} else if (typeof source === 'object' && source.uri) {
Image.getSize(source.uri, (w, height) => {
const aspectRatio = w / height;
const calculatedHeight = width / aspectRatio;
setImgHight(calculatedHeight)
});
}
}, [source])
return (
<ExpoImage
style={[style, {
width: width,
height: imgHeight,
alignSelf: 'center'
}]}
source={source}
/>
)
}
export default AutoHeightImage