ProfileScreenでプロフィールの写真を撮影UIを押したときに、CameraScreenのカメラが起動せずに困っています。。
connectActionSheetライブラリで写真を撮影とアルバムから選択というUIを表示させ、写真を撮影UIを押すと別ファイルで定義されているCameraScreenが実行されるようにしたいです。
ProfileScreen
import React from "react"; import { ScrollView, View, Text, Button, Alert, StyleSheet, Image, TextInput, Keyboard, KeyboardAvoidingView, Platform } from "react-native"; import styled from "styled-components"; // camera起動 import CameraScreen from "../components/Camera"; // camera-roll起動 import * as ImagePicker from "expo-image-picker"; // camera-roll、cameraの選択UIライブラリ import { connectActionSheet } from "@expo/react-native-action-sheet"; class Profile extends React.Component { // ここでカメラコンポーネントを呼び出してほしい。。 _onCamera = () => { CameraScreen; }; // 写真を撮影、アルバムから選択の項目を定義 _onOpenActionSheet = () => { const options = ["写真を撮影", "アルバムから選択", "キャンセル"]; const cancelButtonIndex = 2; this.props.showActionSheetWithOptions( { options, cancelButtonIndex }, buttonIndex => { if (buttonIndex === 0) { console.log("写真を撮影が押されたよ!"); this._onCamera(); } else if (buttonIndex === 1) { console.log("アルバムから選択が押されたよ!"); // アルバムから選択は、まだ未定義です this._onCamerarollAction(); } else { console.log("キャンセルが押されたよ!"); } } ); }; render(){ return( <ImageArea onPress={this._onOpenActionSheet}> <Image style={styles.image} source={require("../assets/person1.png")} /> </ImageArea> ); } } // Profileコンポーネントでaciton-sheetを利用できるよう定義 const ProfileScreen = connectActionSheet(Profile); export default ProfileScreen; //CSSは省いてます。
CameraScreen
import React from "react"; import { Text, View, TouchableOpacity } from "react-native"; import * as Permissions from "expo-permissions"; import { Camera } from "expo-camera"; export default class CameraScreen extends React.Component { state = { hasCameraPermission: null, type: Camera.Constants.Type.back }; // カメラの利用許可をユーザーに尋ねる async componentDidMount() { const { status } = await Permissions.askAsync(Permissions.CAMERA); this.setState({ hasCameraPermission: status === "granted" }); } render() { const { hasCameraPermission } = this.state; if (hasCameraPermission === null) { return <View />; } else if (hasCameraPermission === false) { return <Text>No access to camera</Text>; } else { return ( <View style={{ flex: 1 }}> <Camera style={{ flex: 1 }} type={this.state.type}> <View style={{ flex: 1, backgroundColor: "transparent", flexDirection: "row" }} > <TouchableOpacity style={{ flex: 0.1, alignSelf: "flex-end", alignItems: "center" }} onPress={() => { this.setState({ type: this.state.type === Camera.Constants.Type.back ? Camera.Constants.Type.front : Camera.Constants.Type.back }); }} > <Text style={{ fontSize: 18, marginBottom: 10, color: "white" }} > {" "} Flip{" "} </Text> </TouchableOpacity> </View> </Camera> </View> ); } } }
この画像の写真を撮影を押したら、、、
このようなカメラが起動するようにしたいです。(カメラの利用許可UIが先に表示されますが、、)
そもそものCameraScreenコンポーネントの読み込み方が間違っている気はするんですが、その解決案が見つからないので相談しました。
あなたの回答
tips
プレビュー