Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use a captured image with react-native-camera

I can get the react-native-camera module to access the camera and save an image. However, I can't figure out how to display this image to the user.

What I'm trying:

Here I take the picture. This generates what looks to be a .jpg file in assets-library://....

    _takePicture() {
        var self = this;
        this.refs.cam.capture(function(err, data) {
          this.setState({photo: data});
          console.log(err, data);
          // data is "assets-library://asset/asset.JPG?id=########-####-####-####-##########&ext=JPG"
          console.log('just took a picture');
        }); 
    }

However, If I try to render the image:

    render: function() {
      return(
         <Image style={styles.image} source={{uri: this.state.photo}}/>
      );
    }

I get this error:

No suitable image URL loader found for assets-library://asset/asset.JPG?id=.......

How can I take a photo, save it to the current state of my application, and render it?

like image 660
YPCrumble Avatar asked Oct 15 '15 17:10

YPCrumble


People also ask

How do I display a selected image in react-native?

To display a image selected from file input in React, we can call the URL. createObjectURL with the selected file object to and set the returned value as the value of the src prop of the img element. We define the img state which we use as the value of the src prop of the img element.

How do you click a photo with the react-native camera in Reactjs?

You just have to import the core React Native components such as View and Alert as well as RNCamera from react-native-camera . Then, create a class component App that is going to render the JSX that uses a hardware camera on the device's screen. This going to be done by wrapping the RNCamera component inside a View .

Can you load an image in react-native?

Background images in React Native CSS is typically the language used to add background images, but React Native provides an ImageBackground component that makes similar functionality available in web applications. The ImageBackground component also accepts the same props that the Image component accepts.


2 Answers

The solution was to enable the save to disk option vs. the save to cameraRoll option:

<Camera
  captureTarget={Camera.constants.CaptureTarget.disk}
  // Rest of Camera options here
/>
like image 120
YPCrumble Avatar answered Oct 04 '22 04:10

YPCrumble


So, I was using the @YPCrumble answer for some time.

But now I have to save the image in my camera roll.

If anyone want to continue saving in camera roll, you have to manually link RTCCameraRoll library.

Documentation to link library here:

https://facebook.github.io/react-native/docs/linking-libraries-ios.html#manual-linking

It is so simple:

You can find the RCTImage.xcodeproj in your node_modules/react-native/Libraries/CameraRoll

Drag and drop this file inside Libraries folder in your XCode project.

After that, click in your main project, and find in the right pane "Build Phases".

Inside "Link Binary With Libraries", drag and drop the file called "libRCTCameraRoll.a" from left pane -> Libraries -> RTCCameraRoll.xcodeproj -> Products

like image 44
Luis Antonio Pestana Avatar answered Oct 04 '22 04:10

Luis Antonio Pestana