v8.101.0

Setting up Pintura Image Editor with React Native

For a quick start use the React Native Expo example project project as a guideline.

Pintura also works without Expo, but as the React Native team advises using Expo, and setting up React Native without a framework can be incredibly tricky, the non-framework React Native integration has been deprecated.

Installation

Pintura is a web based image editor and thus makes extensive use of web technology, this means it cannot be rendered as a native component. The React Native version of Pintura is rendered in a WebView and communication between the WebView and the app itself is abstracted away by a proxy component.

Because Pintura runs in a WebView all communication has to be stringified. This means that image and video files have to be sent as Data URLs. The component does this by sending chunks of bytes to and from the WebView, this prevents huge amounts of memory being allocated.

Of course this still is more memory intensive than passing a simple file reference.

To conserve memory the proxy component removes the src and dest property from the value returned by the onLoad callback and removes the src property from the value returned by the onProcess callback.

The React Native <PinturaEditor> component loads all the editor default settings and plugins and can then be used in the same manner as the non-native React component.

After installing or updating the @pqina/pintura or @pqina/pintura-video modules we need to run npm rebuild to rebuild the internal WebView component html.

Please note that after installing @pqina/pintura-video, and running npm rebuild no additional configuration is needed, the editor can then read and write video data.

On React Native the Pintura video extension defaults to using Mediabunny, after installing or updating Mediabunny it we need to run npm rebuild to update the WebView component html.

Usage

import React, { useEffect, useState } from "react";
import { useAssets } from "expo-asset";
import PinturaEditor from "@pqina/react-native-expo-pintura";

const editorStyle = { width: 300, height: 600 };

export default function App() {

    const [image, error] = useAssets(require("./assets/image.jpeg"));

    const [editorSource, setEditorSource] = useState(undefined);

    useEffect(() => {
        if (!assets?.length) {
            return;
        }
        setEditorSource(assets[0].localUri);
    }, [assets]);

    return (
        <View>
            <PinturaEditor
                style={editorStyle}
                src={editorSource}
                imageCropAspectRatio={1} />
        </View>
    );
}

export default App;

Events and properties

To handle events we can use camelcase props. This means that to listen for the Pintura Image Editor load event we have to set the onLoad prop.

<PinturaEditor onLoad={handleEvent} />

Properties can be used as we would with the normal JavaScript version of the editor.

<PinturaEditor imageCropAspectRatio={1} />

Methods

To run editor methods we need to get a ref to the editor component. We can do so using the useRef hook. When the reference is set up we can access the editor instance.

export default function Example() {
    // get a reference to the PinturaEditor component
    const componentRef = useRef(null);

    const handleUndo = () => {
        // get reference to editor instance
        const { editor } = componentRef.current;

        // run history.undo()
        editor.history.undo();
    };

    return (
        <View>
            <Button onPress={handleUndo}>Undo</Button>

            <PinturaEditor ref={componentRef} />
        </View>
    );
}

Styles

To pass custom styles to the editor we can use the styleRules property. This property passes the styles to the webview and adds them to the page.

export default function Example() {
    return (
        <View>
            <PinturaEditor
                styleRules={`
                    .pintura-editor {
                        --color-background: 0, 0, 0;
                        --color-foreground: 255, 255, 255;
                    }
                `}
            />
        </View>
    );
}

Next steps

With the editor set up, we can continue to configure the editor to our liking by adjusting the available options exposed by the editor API