react-native-nitro-image-pipeline 1.1.0 → 1.2.1
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 +84 -8
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/HybridNitroImagePipeline.kt +36 -15
- package/android/src/main/java/com/margelo/nitro/nitroimagepipeline/transform/ResizeTransformation.kt +58 -0
- package/ios/HybridNitroImagePipeline.swift +54 -27
- package/ios/RoundedCornersProcessor.swift +6 -3
- package/lib/commonjs/NitroImagePipeline.js +9 -0
- package/lib/commonjs/NitroImagePipeline.js.map +1 -0
- package/lib/commonjs/PipelineImage.js +111 -0
- package/lib/commonjs/PipelineImage.js.map +1 -0
- package/lib/commonjs/index.js +40 -81
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/resizeForStyle.js +68 -0
- package/lib/commonjs/resizeForStyle.js.map +1 -0
- package/lib/commonjs/useImage.js +95 -0
- package/lib/commonjs/useImage.js.map +1 -0
- package/lib/module/NitroImagePipeline.js +5 -0
- package/lib/module/NitroImagePipeline.js.map +1 -0
- package/lib/module/PipelineImage.js +107 -0
- package/lib/module/PipelineImage.js.map +1 -0
- package/lib/module/index.js +4 -79
- package/lib/module/index.js.map +1 -1
- package/lib/module/resizeForStyle.js +62 -0
- package/lib/module/resizeForStyle.js.map +1 -0
- package/lib/module/useImage.js +91 -0
- package/lib/module/useImage.js.map +1 -0
- package/lib/typescript/src/NitroImagePipeline.d.ts +3 -0
- package/lib/typescript/src/NitroImagePipeline.d.ts.map +1 -0
- package/lib/typescript/src/PipelineImage.d.ts +57 -0
- package/lib/typescript/src/PipelineImage.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +5 -38
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/resizeForStyle.d.ts +30 -0
- package/lib/typescript/src/resizeForStyle.d.ts.map +1 -0
- package/lib/typescript/src/specs/nitro-image-toolkit.nitro.d.ts +33 -5
- package/lib/typescript/src/specs/nitro-image-toolkit.nitro.d.ts.map +1 -1
- package/lib/typescript/src/useImage.d.ts +53 -0
- package/lib/typescript/src/useImage.d.ts.map +1 -0
- package/nitrogen/generated/android/c++/JHybridNitroImagePipelineSpec.cpp +4 -0
- package/nitrogen/generated/android/c++/JOptions.hpp +9 -3
- package/nitrogen/generated/android/c++/JResizeOptions.hpp +61 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/Options.kt +9 -4
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroimagepipeline/ResizeOptions.kt +56 -0
- package/nitrogen/generated/ios/NitroImagePipeline-Swift-Cxx-Bridge.hpp +18 -0
- package/nitrogen/generated/ios/NitroImagePipeline-Swift-Cxx-Umbrella.hpp +3 -0
- package/nitrogen/generated/ios/c++/HybridNitroImagePipelineSpecSwift.hpp +3 -0
- package/nitrogen/generated/ios/swift/Options.swift +12 -1
- package/nitrogen/generated/ios/swift/ResizeOptions.swift +34 -0
- package/nitrogen/generated/shared/c++/Options.hpp +9 -2
- package/nitrogen/generated/shared/c++/ResizeOptions.hpp +87 -0
- package/package.json +1 -1
- package/src/NitroImagePipeline.ts +6 -0
- package/src/PipelineImage.tsx +156 -0
- package/src/index.ts +10 -123
- package/src/resizeForStyle.ts +96 -0
- package/src/specs/nitro-image-toolkit.nitro.ts +34 -5
- package/src/useImage.ts +149 -0
package/src/useImage.ts
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react';
|
|
2
|
+
import type { Image } from 'react-native-nitro-image';
|
|
3
|
+
|
|
4
|
+
import { NitroImagePipeline } from './NitroImagePipeline';
|
|
5
|
+
import type {
|
|
6
|
+
CacheOption,
|
|
7
|
+
CornerRadii,
|
|
8
|
+
ResizeOptions,
|
|
9
|
+
} from './specs/nitro-image-toolkit.nitro';
|
|
10
|
+
|
|
11
|
+
type Result =
|
|
12
|
+
// Loading State
|
|
13
|
+
| {
|
|
14
|
+
image: undefined;
|
|
15
|
+
error: undefined;
|
|
16
|
+
}
|
|
17
|
+
// Loaded state
|
|
18
|
+
| {
|
|
19
|
+
image: Image;
|
|
20
|
+
error: undefined;
|
|
21
|
+
}
|
|
22
|
+
// Error state
|
|
23
|
+
| {
|
|
24
|
+
image: undefined;
|
|
25
|
+
error: Error;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* A hook to asynchronously load an image from the
|
|
30
|
+
* given {@linkcode AsyncImageSource} into memory.
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const { image, error } = useImage({ filePath: '/tmp/image.jpg' })
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
export function useImage({
|
|
37
|
+
url,
|
|
38
|
+
blur = 0,
|
|
39
|
+
cornerRadius = 0,
|
|
40
|
+
resize,
|
|
41
|
+
cache,
|
|
42
|
+
enabled = true,
|
|
43
|
+
}: {
|
|
44
|
+
url: string;
|
|
45
|
+
/**
|
|
46
|
+
* Gaussian blur strength, as the standard deviation (sigma) of the blur in
|
|
47
|
+
* source-image pixels (of the resized bitmap when `resize` is set). Matches
|
|
48
|
+
* across iOS and Android; roughly half of React Native's `blurRadius`.
|
|
49
|
+
*/
|
|
50
|
+
blur?: number;
|
|
51
|
+
/**
|
|
52
|
+
* Corner radius in pixels of the loaded bitmap. Pass a single number for
|
|
53
|
+
* uniform rounding, or per-corner radii (inline object literals are fine —
|
|
54
|
+
* the hook compares the radii by value, not identity). Pair with `resize`
|
|
55
|
+
* so the radii apply at the size you display instead of the source size.
|
|
56
|
+
*/
|
|
57
|
+
cornerRadius?: number | CornerRadii;
|
|
58
|
+
/**
|
|
59
|
+
* Resize the bitmap to exactly this size in pixels (aspect-fill,
|
|
60
|
+
* center-crop) before blur/rounding. Typically your display size in points
|
|
61
|
+
* multiplied by `PixelRatio.get()`. Inline object literals are fine.
|
|
62
|
+
*/
|
|
63
|
+
resize?: ResizeOptions;
|
|
64
|
+
cache?: CacheOption;
|
|
65
|
+
/**
|
|
66
|
+
* When `false`, no request is made and the result stays in the loading
|
|
67
|
+
* state (or keeps the current image if `url` is unchanged) until it becomes
|
|
68
|
+
* `true`. Use it to defer loading until inputs such as a layout size are
|
|
69
|
+
* known.
|
|
70
|
+
* @default true
|
|
71
|
+
*/
|
|
72
|
+
enabled?: boolean;
|
|
73
|
+
}): Result {
|
|
74
|
+
const [image, setImage] = useState<Result>({
|
|
75
|
+
image: undefined,
|
|
76
|
+
error: undefined,
|
|
77
|
+
});
|
|
78
|
+
const loadedUrlRef = useRef(url);
|
|
79
|
+
|
|
80
|
+
// Split the option into primitives so an inline `{ topLeft: 24, ... }`
|
|
81
|
+
// literal (new identity every render) doesn't re-trigger the effect.
|
|
82
|
+
const isUniformRadius = typeof cornerRadius === 'number';
|
|
83
|
+
const uniformRadius = isUniformRadius ? cornerRadius : 0;
|
|
84
|
+
const {
|
|
85
|
+
topLeft = 0,
|
|
86
|
+
topRight = 0,
|
|
87
|
+
bottomLeft = 0,
|
|
88
|
+
bottomRight = 0,
|
|
89
|
+
} = isUniformRadius ? {} : cornerRadius;
|
|
90
|
+
const resizeWidth = resize?.width ?? 0;
|
|
91
|
+
const resizeHeight = resize?.height ?? 0;
|
|
92
|
+
|
|
93
|
+
useEffect(() => {
|
|
94
|
+
let cancelled = false;
|
|
95
|
+
// Only reset to the loading state when the URL changes; for same-URL
|
|
96
|
+
// param tweaks (blur/cornerRadius/cache) keep showing the current image
|
|
97
|
+
// until the new variant resolves, to avoid flashing empty.
|
|
98
|
+
if (loadedUrlRef.current !== url) {
|
|
99
|
+
loadedUrlRef.current = url;
|
|
100
|
+
setImage({ image: undefined, error: undefined });
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (enabled) {
|
|
104
|
+
(async () => {
|
|
105
|
+
try {
|
|
106
|
+
const result = await NitroImagePipeline.loadImage(url, {
|
|
107
|
+
blur,
|
|
108
|
+
cornerRadius: isUniformRadius
|
|
109
|
+
? uniformRadius
|
|
110
|
+
: { topLeft, topRight, bottomLeft, bottomRight },
|
|
111
|
+
resize:
|
|
112
|
+
resizeWidth > 0 && resizeHeight > 0
|
|
113
|
+
? { width: resizeWidth, height: resizeHeight }
|
|
114
|
+
: undefined,
|
|
115
|
+
cache,
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
if (!cancelled) {
|
|
119
|
+
setImage({ image: result, error: undefined });
|
|
120
|
+
}
|
|
121
|
+
} catch (e) {
|
|
122
|
+
const error = e instanceof Error ? e : new Error(`${e}`);
|
|
123
|
+
if (!cancelled) {
|
|
124
|
+
setImage({ image: undefined, error: error });
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
})();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return () => {
|
|
131
|
+
cancelled = true;
|
|
132
|
+
};
|
|
133
|
+
}, [
|
|
134
|
+
url,
|
|
135
|
+
blur,
|
|
136
|
+
isUniformRadius,
|
|
137
|
+
uniformRadius,
|
|
138
|
+
topLeft,
|
|
139
|
+
topRight,
|
|
140
|
+
bottomLeft,
|
|
141
|
+
bottomRight,
|
|
142
|
+
resizeWidth,
|
|
143
|
+
resizeHeight,
|
|
144
|
+
cache,
|
|
145
|
+
enabled,
|
|
146
|
+
]);
|
|
147
|
+
|
|
148
|
+
return image;
|
|
149
|
+
}
|