Taking a picture does nothing using expo-camera

  1. SDK Version: 4.10.0
  2. Platform: iOS
  3. Tag: camera

Hello, I am fairly new to this so I may be missing something simple. I wanted to create an app to take a picture and then save it in my camera roll using iPhone. Using the code below, I get nothing, not even my console.log had logged anything

export default function App() {

  const [hasPermission, setHasPermission] = useState(null);
  
  useEffect(() => {
    (async () => {
      const { status } = await Camera.requestPermissionsAsync();
      const { status2 } = await MediaLibrary.requestPermissionsAsync();
      setHasPermission(status === 'granted');
    })();
  }, []);

  if (hasPermission === null) {
    return <View />;
  }
  if (hasPermission === false) {
    return <Text>No access to camera or photos</Text>;
  }
}

function Cam() {

   const takePicture = async () => {
    const photo = await camera.takePictureAsync()
    MediaLibrary.saveToLibraryAsync(photo.uri)
  }

  const [type, setType] = useState(Camera.Constants.Type.back);
  return (
    <View style={styles.container}>
      <Camera style={styles.camera} type={type}>
        <View style={styles.buttonContainer}>
          <TouchableOpacity
            onPress={takePicture}
            style={{
            width: 70,
            height: 70,
            bottom: 0,
            borderRadius: 50,
            backgroundColor: '#fff',
            alignItems: 'center',
            alignSelf: 'center',
          }}
          >
          </TouchableOpacity>
          <TouchableOpacity
            style={styles.button}
            onPress={() => {
              setType(
                type === Camera.Constants.Type.back
                  ? Camera.Constants.Type.front
                  : Camera.Constants.Type.back
              );
            }}>
            <Text style={{transform: [{ rotate: '90deg' }], color: 'white', textAlign: 'left'}}> Flip </Text>
          </TouchableOpacity>
        </View>
      </Camera>
    </View>
  );
}

However, if I remove the if (this.camera) {} I get my console.log(“taking picture”) but followed by an error [Unhandled promise rejection: TypeError: undefined is not an object (evaluating ‘_this.camera.takePictureAsync’)]

this.camera isn’t present in the code you posted. I suggest you start with the example from the docs and work from there- Camera - Expo Documentation

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.