最近需要在react native项目中集成相机和相册的功能,于是在网上找了一个好用的第三方插件:react-native-image-picke.
该插件可以同时给iOS和Android两个平台下使用,但是需要配置下各自平台下的文件,github上有详细的文字说明,但是本人对于文字向来无感,所以特地图解记录下配置说明和用法.
1. 首先,安装下该插件:
1 | npm install react- native -image-picker @latest --save |
2. 先别link,先添加进来该库之后 再link.
下面对于各自平台进行配置即可.
iOS:
- In the XCode's "Project navigator", right click on your project's Libraries folder ➜
Add Files to <...>
- Go to
node_modules
➜react-native-image-picker
➜ios
➜ selectRNImagePicker.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
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
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(photoOptions,(response) =>{ console.log( 'response' +response); if (response.didCancel){ return } }) } |
案例:
demo地址:https://github.com/pheromone/mycamera
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· NetPad:一个.NET开源、跨平台的C#编辑器
· PowerShell开发游戏 · 打蜜蜂
· 凌晨三点救火实录:Java内存泄漏的七个神坑,你至少踩过三个!