-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
cache.js
83 lines (76 loc) · 2.07 KB
/
cache.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* @since 2017-04-24 20:50:41
* @author vivaxy
*/
import { Image } from 'react-native';
// undocumented but part of react-native; see
// https://github.com/facebook/react-native/issues/5603#issuecomment-297959695
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
/**
* store with
* key: image
* value: {
* width: 100,
* height: 100,
* }
*/
const cache = new Map();
const getImageSizeFromCache = (image) => {
if (typeof image === 'number') {
return cache.get(image);
} else {
return cache.get(image.uri);
}
};
const loadImageSize = (image) => {
return new Promise((resolve, reject) => {
//number indicates import X or require(X) was used (i.e. local file)
if (typeof image === 'number') {
const { width, height } = resolveAssetSource(image);
resolve({ width, height });
} else {
Image.getSize(
image.uri,
(width, height) => {
// success
resolve({ width, height });
},
reject
);
}
});
};
export const getImageSizeFitWidthFromCache = (image, toWidth, maxHeight) => {
const size = getImageSizeFromCache(image);
if (size) {
const { width, height } = size;
if (!width || !height) return { width: 0, height: 0 };
const scaledHeight = (toWidth * height) / width;
return {
width: toWidth,
height: scaledHeight > maxHeight ? maxHeight : scaledHeight
};
}
return {};
};
const getImageSizeMaybeFromCache = async (image) => {
let size = getImageSizeFromCache(image);
if (!size) {
size = await loadImageSize(image);
if (typeof image === 'number') {
cache.set(image, size);
} else {
cache.set(image.uri, size);
}
}
return size;
};
export const getImageSizeFitWidth = async (image, toWidth, maxHeight) => {
const { width, height } = await getImageSizeMaybeFromCache(image);
if (!width || !height) return { width: 0, height: 0 };
const scaledHeight = (toWidth * height) / width;
return {
width: toWidth,
height: scaledHeight > maxHeight ? maxHeight : scaledHeight
};
};