- SDK Version: 4.10.0
- Platform: iOS
- 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’)]