react-native-nitro-image-pipeline 1.5.0 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +56 -7
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/HybridNitroImagePipeline.kt +27 -2
- package/ios/HybridNitroImagePipeline.swift +47 -9
- package/ios/PipelineImageLoader.swift +2 -2
- package/lib/commonjs/NativePipelineImage.js +2 -1
- package/lib/commonjs/NativePipelineImage.js.map +1 -1
- package/lib/commonjs/PipelineImage.js.map +1 -1
- package/lib/commonjs/index.js +13 -0
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/resolveImageSource.js +89 -0
- package/lib/commonjs/resolveImageSource.js.map +1 -0
- package/lib/commonjs/useImage.js +8 -4
- package/lib/commonjs/useImage.js.map +1 -1
- package/lib/commonjs/usePipelineImageLoader.js +8 -3
- package/lib/commonjs/usePipelineImageLoader.js.map +1 -1
- package/lib/module/NativePipelineImage.js +2 -1
- package/lib/module/NativePipelineImage.js.map +1 -1
- package/lib/module/PipelineImage.js.map +1 -1
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/resolveImageSource.js +83 -0
- package/lib/module/resolveImageSource.js.map +1 -0
- package/lib/module/useImage.js +8 -4
- package/lib/module/useImage.js.map +1 -1
- package/lib/module/usePipelineImageLoader.js +8 -3
- package/lib/module/usePipelineImageLoader.js.map +1 -1
- package/lib/typescript/src/NativePipelineImage.d.ts +7 -2
- package/lib/typescript/src/NativePipelineImage.d.ts.map +1 -1
- package/lib/typescript/src/PipelineImage.d.ts +7 -2
- package/lib/typescript/src/PipelineImage.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +1 -0
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/resolveImageSource.d.ts +56 -0
- package/lib/typescript/src/resolveImageSource.d.ts.map +1 -0
- package/lib/typescript/src/specs/nitro-image-toolkit.nitro.d.ts +12 -0
- package/lib/typescript/src/specs/nitro-image-toolkit.nitro.d.ts.map +1 -1
- package/lib/typescript/src/useImage.d.ts +10 -4
- package/lib/typescript/src/useImage.d.ts.map +1 -1
- package/lib/typescript/src/usePipelineImageLoader.d.ts +4 -2
- package/lib/typescript/src/usePipelineImageLoader.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/NativePipelineImage.tsx +8 -3
- package/src/PipelineImage.tsx +7 -2
- package/src/index.ts +5 -0
- package/src/resolveImageSource.ts +83 -0
- package/src/specs/nitro-image-toolkit.nitro.ts +12 -0
- package/src/useImage.ts +26 -15
- package/src/usePipelineImageLoader.ts +11 -3
package/src/useImage.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { useEffect, useRef, useState } from 'react';
|
|
|
2
2
|
import type { Image } from 'react-native-nitro-image';
|
|
3
3
|
|
|
4
4
|
import { NitroImagePipeline } from './NitroImagePipeline';
|
|
5
|
+
import { type ImageSource, resolveImageUrl } from './resolveImageSource';
|
|
5
6
|
import type {
|
|
6
7
|
CacheOption,
|
|
7
8
|
CornerRadii,
|
|
@@ -26,11 +27,12 @@ type Result =
|
|
|
26
27
|
};
|
|
27
28
|
|
|
28
29
|
/**
|
|
29
|
-
* A hook to asynchronously load an image from the
|
|
30
|
-
*
|
|
30
|
+
* A hook to asynchronously load an image from `url` through the pipeline
|
|
31
|
+
* into memory.
|
|
31
32
|
* @example
|
|
32
33
|
* ```ts
|
|
33
|
-
* const { image, error } = useImage({
|
|
34
|
+
* const { image, error } = useImage({ url: 'https://example.com/photo.jpg' });
|
|
35
|
+
* const { image: logo } = useImage({ url: require('./logo.png') });
|
|
34
36
|
* ```
|
|
35
37
|
*/
|
|
36
38
|
export function useImage({
|
|
@@ -41,7 +43,11 @@ export function useImage({
|
|
|
41
43
|
cache,
|
|
42
44
|
enabled = true,
|
|
43
45
|
}: {
|
|
44
|
-
|
|
46
|
+
/**
|
|
47
|
+
* The image to load: a URL string (`https://`, `file://`, an absolute path)
|
|
48
|
+
* or a `require()`d asset — see {@linkcode ImageSource}.
|
|
49
|
+
*/
|
|
50
|
+
url: ImageSource;
|
|
45
51
|
/**
|
|
46
52
|
* Gaussian blur strength, as the standard deviation (sigma) of the blur in
|
|
47
53
|
* source-image pixels (of the resized bitmap when `resize` is set). Matches
|
|
@@ -103,17 +109,22 @@ export function useImage({
|
|
|
103
109
|
if (enabled) {
|
|
104
110
|
(async () => {
|
|
105
111
|
try {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
?
|
|
114
|
-
:
|
|
115
|
-
|
|
116
|
-
|
|
112
|
+
// Resolved inside the try so an unregistered `require()` id
|
|
113
|
+
// surfaces as the error state instead of throwing from the effect.
|
|
114
|
+
const result = await NitroImagePipeline.loadImage(
|
|
115
|
+
resolveImageUrl(url),
|
|
116
|
+
{
|
|
117
|
+
blur,
|
|
118
|
+
cornerRadius: isUniformRadius
|
|
119
|
+
? uniformRadius
|
|
120
|
+
: { topLeft, topRight, bottomLeft, bottomRight },
|
|
121
|
+
resize:
|
|
122
|
+
resizeWidth > 0 && resizeHeight > 0
|
|
123
|
+
? { width: resizeWidth, height: resizeHeight }
|
|
124
|
+
: undefined,
|
|
125
|
+
cache,
|
|
126
|
+
},
|
|
127
|
+
);
|
|
117
128
|
|
|
118
129
|
if (!cancelled) {
|
|
119
130
|
setImage({ image: result, error: undefined });
|
|
@@ -2,13 +2,18 @@ import { useMemo } from 'react';
|
|
|
2
2
|
import type { ImageLoader } from 'react-native-nitro-image';
|
|
3
3
|
|
|
4
4
|
import { NitroImagePipeline } from './NitroImagePipeline';
|
|
5
|
+
import {
|
|
6
|
+
type ImageSource,
|
|
7
|
+
resolveImageUrlOrFallback,
|
|
8
|
+
} from './resolveImageSource';
|
|
5
9
|
import type {
|
|
6
10
|
CornerRadii,
|
|
7
11
|
ViewOptions,
|
|
8
12
|
} from './specs/nitro-image-toolkit.nitro';
|
|
9
13
|
|
|
10
14
|
/**
|
|
11
|
-
* Creates (and memoizes) an {@linkcode ImageLoader} for `
|
|
15
|
+
* Creates (and memoizes) an {@linkcode ImageLoader} for `source` — a URL
|
|
16
|
+
* string or a `require()`d asset, see {@linkcode ImageSource} — to pass to
|
|
12
17
|
* `<NativeNitroImage image={...} />`. The view drives it entirely natively:
|
|
13
18
|
* the request starts when the view attaches — at the view's laid-out size,
|
|
14
19
|
* with no JS round trips — and is cancelled when it detaches. See
|
|
@@ -19,7 +24,7 @@ import type {
|
|
|
19
24
|
* literals are fine — options are compared by value, not identity.
|
|
20
25
|
*/
|
|
21
26
|
export function usePipelineImageLoader(
|
|
22
|
-
|
|
27
|
+
source: ImageSource,
|
|
23
28
|
options?: ViewOptions,
|
|
24
29
|
): ImageLoader {
|
|
25
30
|
// Split the options into primitives (like useImage does) so an inline
|
|
@@ -41,6 +46,9 @@ export function usePipelineImageLoader(
|
|
|
41
46
|
const resizeHeight = options?.resize?.height;
|
|
42
47
|
|
|
43
48
|
return useMemo(() => {
|
|
49
|
+
// Runs during render, so an unregistered `require()` id must not throw:
|
|
50
|
+
// it becomes a URL the native loader fails on at load time instead.
|
|
51
|
+
const url = resolveImageUrlOrFallback(source);
|
|
44
52
|
const cornerRadiusOption: number | CornerRadii | undefined = isUniformRadius
|
|
45
53
|
? uniformRadius
|
|
46
54
|
: hasCornerObject
|
|
@@ -66,7 +74,7 @@ export function usePipelineImageLoader(
|
|
|
66
74
|
});
|
|
67
75
|
return loader;
|
|
68
76
|
}, [
|
|
69
|
-
|
|
77
|
+
source,
|
|
70
78
|
blur,
|
|
71
79
|
cache,
|
|
72
80
|
isUniformRadius,
|