๐๏ธ This library is work in progress! ๐๏ธ
A writeable in-memory Image JSI Host Object.
JSI-Image is a modern library that provides Image primitives for the native iOS and Android Platforms, neatly packaged together in one single fast JavaScript API.
There are 3 ways to create a JSI-Image instance:
- Load from a file
- Load from a Web-URL
- Returned by another library, such as VisionCamera's
takePhoto(...)
function.
Traditionally, Images in React Native could not be handled efficiently. To demonstrate this, let's take a look at how a Camera library might take a photo:
- [js] User taps capture button,
takePhoto(...)
is called. - [native] Camera takes a photo. The library now has
UIImage
instance (photo) in-memory. - [native] Library creates a new file on disk. (slow! ๐)
- [native] Library writes the
UIImage
instance to the file. (slow! ๐) - [native] Library returns the path to the file to the caller (JS)
- [js] App now navigates to the "captured media" screen to display the media.
- [js] App passes the file path to a
<FastImage>
component. - [native]
<FastImage>
component has to load the image from file. (slow! ๐) - [native]
<FastImage>
component then displays theUIImage
from the file.
With JSI-Image, all the unnecessary slow file operations can be skipped, since the Image can be passed around in-memory.
- [js] User taps capture button,
takePhoto(...)
is called. - [native] Camera takes a photo. The library now has
UIImage
instance (photo) in-memory. - [native] Library returns the
UIImage
instance to the caller (JS) (fast! ๐ฅ) - [js] App now navigates to the "captured media" screen to display the media.
- [js] App passes the in-memory
Image
instance to a<FastImage>
component. - [native]
<FastImage>
component then displays the already in-memoryUIImage
instance. (fast! ๐ฅ)
[log] Successfully took photo in 312ms!
[log] Successfully took photo in 95ms!
JSI-Image improved capture speed (takePhoto(...)
) by more than 3x!
These improvements are even greater at more complicated image processing, such as rotating an image, applying image filters, resizing images, etc.
yarn add react-native-jsi-image
cd ios && pod install
import { loadImageFromUrl } from "react-native-jsi-image"
const image = await loadImageFromUrl('https://...')
console.log(`Successfully loaded ${image.width} x ${image.height} image!`)
import { loadImageFromFile } from "react-native-jsi-image"
const image = await loadImageFromFile('file:///Users/Marc/image.png')
console.log(`Successfully loaded ${image.width} x ${image.height} image!`)
const image = ...
const size = image.width * image.height
const realSize = size * image.scale
const orientation = image.orientation
for (const pixel of image.data) {
console.log(`Pixel: ${pixel}`)
}
const image = ...
console.log(image.isFlipped) // false
const flipped = image.flip()
console.log(flipped.isFlipped) // true
if (image.orientation === "up") {
// rotates image in-memory
image.orientation = "right"
}
let image = ...
image = rotateImageCorrectly(image)
await image.save('file:///tmp/temp-image.png') // or .jpg
To use JSI-Image in your native library, your functions must be JSI functions.
In your JSI Module:
#include <JsiImage/ImageHostObject.h>
// ...
jsi::Value myFunction(jsi::Runtime& runtime,
jsi::Value& thisArg,
jsi::Value* arguments,
size_t count) {
auto imageHostObject = arguments[0].asObject(runtime).asHostObject<ImageHostObject>(runtime);
auto uiImage = imageHostObject->image;
// use uiImage here
}
In your TypeScript declaration:
import { Image } from 'react-native-jsi-image'
export function myFunction(image: Image): void
In your JSI Module:
#include <JsiImage/ImageHostObject.h>
// ...
jsi::Value myFunction(jsi::Runtime& runtime,
jsi::Value& thisArg,
jsi::Value* arguments,
size_t count) {
UIImage* image = // ...
auto instance = std::make_shared<ImageHostObject>(image, promiseVendor);
return jsi::Object::createFromHostObject(runtime, instance);
}
In your TypeScript declaration:
import { Image } from 'react-native-jsi-image'
export function myFunction(): Image
See the contributing guide to learn how to contribute to the repository and the development workflow.
MIT