라이브러리를 설치한다.
npm i react-native-image-picker
🍪 android 권한 설정
android/app/src/main/AndroidManifest.xml 에 추가한다.
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
🍩 ios 권한 설정
ios/App_이름/info.plist 에 추가한다.
<key>NSPhotoLibraryUsageDescription</key>
<string>This app requires access to the photo library.</string>
<key>NSMicrophoneUsageDescription</key>
<string>This app does not require access to the microphone.</string>
<key>NSCameraUsageDescription</key>
<string>This app requires access to the camera.</string>
또한, 라이브러리 설치 후 pod install을 한다.
- 사용 예시 (카메라로 사진을 찍은 뒤 이미지 사용)
import React, {useState} from 'react';
import {View, Text, StyleSheet, Image, Button} from 'react-native';
import {launchCamera} from 'react-native-image-picker';
const App = () => {
const [avatar, setAvatar] = useState('');
const addImage = async () => {
await launchCamera({}, response => {
setAvatar(response.assets[0].uri);
});
};
return (
<View style={styles.container}>
<Text>Hello</Text>
{avatar?.length > 0 ? (
<Image source={{uri: avatar}} style={styles.avatar} />
) : null}
<Button title="Add an image" onPress={() => addImage()} />
</View>
);
}
export default App;
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#e4ab26',
},
avatar: {
width: '100%',
height: 400,
},
});
- 사용 예시 (갤러리에서 기존의 이미지 선택)
import React, {useState} from 'react';
import {View, Text, StyleSheet, Image, Button} from 'react-native';
import {launchImageLibrary} from 'react-native-image-picker';
const App = () => {
const [avatar, setAvatar] = useState('');
const addImage = async () => {
await launchImageLibrary({}, response => {
setAvatar(response.assets[0].uri);
});
};
return (
<View style={styles.container}>
<Text>Hello</Text>
{avatar?.length > 0 ? (
<Image source={{uri: avatar}} style={styles.avatar} />
) : null}
<Button title="Add an image" onPress={() => addImage()} />
</View>
);
}
export default App;
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#e4ab26',
},
avatar: {
width: '100%',
height: 400,
},
});
공식 문서 : https://github.com/react-native-image-picker/react-native-image-picker
GitHub - react-native-image-picker/react-native-image-picker: A React Native module that allows you to use native UI to select m
:sunrise_over_mountains: A React Native module that allows you to use native UI to select media from the device library or directly from the camera. - GitHub - react-native-image-picker/react-nativ...
github.com
NSPhotoLibraryUsageDescription key must be present in Info.plist to use camera roll
Recently I started to get this error: NSPhotoLibraryUsageDescription key must be present in Info.plist to use camera roll. I am using React Native to build my app (I am not familiar with ios
stackoverflow.com
'React Native' 카테고리의 다른 글
react-native-config를 사용해서 env 설정하기 (+ ios 캐싱 문제 해결) (0) | 2022.12.20 |
---|---|
React Native 웹뷰 사용하기 (react-native-webview) (0) | 2022.11.21 |
React Native의 Animated 사용하기 (0) | 2022.11.16 |
React Native Routing 설정 (react-navigation 사용하기) (0) | 2022.11.13 |
React Native 안드로이드 에러 (0) | 2022.11.09 |