首先给个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的模拟器不能拍照哦

案例:

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

  1. React Native之Fetch简单封装、获取网络状态

    1.Fetch的使用 fetch的使用非常简单,只需传入请求的url fetch('https://facebook.github.io/react-native/movies.json'); 当然是 ...

  2. React native 之android的图标和启动图片

    哎哎呀呀,上篇说到了react native的IOS的图标和启动图片的设置,其实最主要的是尺寸!相应的尺寸设定好了以后就不会报错了! ok~这篇说的是React native的android的图标和启 ...

  3. React Native - 使用Geolocation进行定位(获取当前位置、监听位置变化)

    1,getCurrentPosition()方法介绍 static getCurrentPosition(geo_success, geo_error?, geo_options? 该方法用于获取当前 ...

  4. react native 的js 文件从哪里获取

    /** * Loading JavaScript code - uncomment the one you want. * * OPTION 1 * Load from development ser ...

  5. [RN] 02 - Overview: React Native Practice of 50 lectures

    观看笔记:零基础 React Native 实战开发视频 50讲 本篇效果:RN入门,整体认识 基本原理 # 1 React到RN # 2 一个简单的例子 /** * Sample React Nat ...

  6. React Native常用组件之ListView组件

    学习iOS开发的同学应该都知道UITableView,几乎每个APP中都有它的存在,而且衍生出各种形态:那么同样,ListView就是在React Native中的tableView,而且更加简单和灵 ...

  7. 【React Native开发】React Native控件之ListView组件解说以及最齐全实例(19)

    ),React Native技术交流4群(458982758).请不要反复加群!欢迎各位大牛,React Native技术爱好者加入交流!同一时候博客左側欢迎微信扫描关注订阅号,移动技术干货,精彩文章 ...

  8. React Native组件的结构和生命周期

    React Native组件的结构和生命周期 一.组件的结构 1.导入引用 可以理解为C++编程中的头文件. 导入引用包括导入react native定义的组件.API,以及自定义的组件. 1.1 导 ...

  9. 携程React Native实践

    React Native(下文简称 RN)开源已经一年多时间,国内各大互联网公司都在使用,携程也在今年 5 月份投入资源开始引入,并推广给多个业务团队使用,本文将会分享我们遇到的一些问题以及我们的优化 ...

随机推荐

  1. Pony 编程语言介绍

    Pony,一种“Rust 遇上 Erlang”的语言,让开发快捷.安全.高效.高并发的程序更简单. 在 Wallaroo Labs,我是工程副总裁,我们正在构建一个用 Pony 编程语言编写的 高性能 ...

  2. strcpy和memcpy,memmove函数的区别

    strcpy和memcpy的区别 strcpy和memcpy都是标准C库函数,它们有下面的特点. strcpy提供了字符串的复制.即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制 ...

  3. 简单的图像显著性区域特征提取方法-----opencv实现LC,AC,FT

    https://blog.csdn.net/cai13160674275/article/details/72991049?locationNum=7&fps=1 四种简单的图像显著性区域特征 ...

  4. 基于STM32F4移植W5500官方驱动库ioLibrary_Driver(转)

    源: 基于STM32F4移植W5500官方驱动库ioLibrary_Driver 参考: 基于STM32+W5500 的Ethernet和Internet移植 Upgrade W5500 Throug ...

  5. Context initialization failed

    Context initialization failed org.springframework.beans.factory.BeanDefinitionStoreException: Invali ...

  6. Oracle数据库的入门之一

    Oracle的介绍: Oracle Database,又名Oracle RDBMS,或简称Oracle.是甲骨文公司的一款关系数据库管理系统.它是在数据库领域一直处于领先地位的产品.可以说Oracle ...

  7. uwsgi 的巨坑

    网上各种找,最后自己猜,猜到了. 必须安装python插件, 网上找的都是不带数字的版本号, 要么找不到要么不行. 我是 3.6.1,尝试加36, 成了. yum install -y uwsgi-p ...

  8. ionic 实现微信朋友圈分享的完整开发流程

    最近开始要着手负责开发ionic的项目了,一直很好奇想实现一个微信朋友圈分享的功能,所以我就网上找了找文章来练手实现,果不其然,找到了几篇,但是发现它们的流程都不太详细,清楚,直接,还有不少坑. 今天 ...

  9. iframe跨域问题:Uncaught DOMException: Blocked a frame with origin解决方法

    在前后端分离的情况下,前台页面将后台页面加载在预留的iframe中:但是遇到了iframe和主窗口双滚动条的情况,由此引申出来了问题: 只保留单个滚动条,那么就要让iframe的高度自适应,而从主页面 ...

  10. SaaS公司融资的「22条军规 」(转)

    编者按:本文是SaaS教父Jason Lemkin的一篇分享.他在文章中分享了他不投资一家公司的22条理由,希望对打算融资和正在融资的你有所启发. 为何有的早期创业公司能成功融资,而有的公司融资却屡遭 ...