RN 使用第三方组件之react-native-image-picker(拍照/从相册获取图片)

首先给个github地址:https://github.com/react-community/react-native-image-picker  英文不行的看下面这个笔记

该插件可以同时给iOS和Android两个平台下使用,但是需要配置下各自平台下的文件

 1.   首先,安装下该插件:

     npm install react-native-image-picker@latest --save

 2.   先别link,先添加进来该库之后 再link.  

 

下面对于各自平台进行配置即可.

   iOS:

      手动添加库 :

  1. In the XCode's "Project navigator", right click on your project's Libraries folder ➜ Add Files to <...>
  2. Go to node_modules ➜ react-native-image-picker ➜ ios ➜ select RNImagePicker.xcodeproj

 =>  打开Xcode打开项目,点击根目录,右键选择 Add Files to 'XXX',选中项目中的该路径下的文件即可:node_modules ➜ react-native-image-picker ➜ ios ➜ select RNImagePicker.xcodeproj

 

      

OK,添加进来之后,再进行 link命令.  react-native link react-native-image-picker 

3.Add RNImagePicker.a to Build Phases -> Link Binary With Libraries

(注意这里的只要出现RNImagePicker.就可以了,跟前面的图标没关系)

 

     

     4.For iOS 10+, Add the NSPhotoLibraryUsageDescription and NSCameraUsageDescription keys to your Info.plist with strings describing why your app needs these permissions

=>对于适配iOS10,需要在info.plist中配置NSPhotoLibraryUsageDescription和NSCameraUsageDescription

(点击加号 选择privacy camera usage Description 和 privacy. PhotoLibraryUsageDescription )

iOS平台配置完毕.

Android:

    1.Add the following lines to android/settings.gradle:

1
2
include  ':react-native-image-picker'
project( ':react-native-image-picker' ).projectDir =  new  File(settingsDir,  '../node_modules/react-native-image-picker/android' )

 

    2.Add the compile line to the dependencies in android/app/build.gradle:

1
2
3
dependencies {
     compile project( ':react-native-image-picker' )
}

  

     3.Add the required permissions in AndroidManifest.xml:

1
2
<uses-permission android:name= "android.permission.CAMERA"  />
<uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />

  

     4.Add the import and link the package in MainApplication.java:   

复制代码
import com.imagepicker.ImagePickerPackage;


.........



 new ImagePickerPackage()
复制代码

 

 

 

OK,安卓这边也配置完毕.

用法:

   配置弹出框信息

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import   ImagePicker from  'react-native-image-picker' //第三方相机
var photoOptions = {
     //底部弹出框选项
     title: '请选择' ,
     cancelButtonTitle: '取消' ,
     takePhotoButtonTitle: '拍照' ,
     chooseFromLibraryButtonTitle: '选择相册' ,
     quality: 0.75 ,
     allowsEditing: true ,
     noData: false ,
     storageOptions: {
         skipBackup:  true ,
         path: 'images'
     }
}
 

点击事件

1
2
3
4
5
6
7
8
cameraAction = () =>{
        
ImagePicker.showImagePicker(options, (response) => {
  console.log('Response = ', response); if (response.didCancel) { console.log('User cancelled image picker'); } else if (response.error) { console.log('ImagePicker Error: ', response.error); } else if (response.customButton) { console.log('User tapped custom button: ', response.customButton); } else { let source = { uri: response.uri }; // You can also display the image using data: // let source = { uri: 'data:image/jpeg;base64,' + response.data }; this.setState({ avatarSource: source }); } });
     }

  

<Image source={this.state.avatarSource} style={styles.uploadAvatar} />

这里注意的是 ios的模拟器不能拍照哦

案例:

转载于:https://www.cnblogs.com/allenxieyusheng/p/7054463.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,关于React Native中图片多选,可以使用react-native-image-picker这个第三方库来实现。以下是使用步骤: 1. 使用npm或者yarn安装该库:`npm install react-native-image-picker` 或者 `yarn add react-native-image-picker` 2. 在iOS中,需要执行`pod install`命令安装相关依赖库。在iOS工程目录下创建Podfile文件,输入以下内容: ``` platform :ios, '9.0' target 'YourApp' do pod 'React', :path => '../node_modules/react-native', :subspecs => [ 'Core', 'CxxBridge', 'RCTText', 'RCTNetwork', 'RCTWebSocket', 'RCTAnimation' ] pod 'yoga', :path => '../node_modules/react-native/ReactCommon/yoga' pod 'react-native-image-picker', :path => '../node_modules/react-native-image-picker' end ``` 3. 在Android中,需要在AndroidManifest.xml文件中添加相关权限: ``` <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> ``` 4. 在React Native组件中导入该库: ``` import ImagePicker from 'react-native-image-picker'; ``` 5. 使用ImagePicker.launchImageLibrary方法选择图片: ``` ImagePicker.launchImageLibrary(options, (response) => { console.log('Response = ', response); if (response.didCancel) { console.log('User cancelled image picker'); } else if (response.error) { console.log('ImagePicker Error: ', response.error); } else if (response.customButton) { console.log('User tapped custom button: ', response.customButton); } else { const source = { uri: response.uri }; // You can also display the image using data: // const source = { uri: 'data:image/jpeg;base64,' + response.data }; this.setState({ avatarSource: source, }); } }); ``` 其中,options参数可以配置图片选择器的一些选项,如选择多张图片等。详细使用方法可以查看该库的官方文档。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值