React Native - 使用图片选择器react-native-image-picker拍照、选照片
http://www.hangge.com/blog/cache/detail_1617.html
React Native - 使用图片选择器react-native-image-picker拍照、选照片
1,react-native-image-picker介绍
- react-native-image-picker 是一个第三方的开源库,它提供了原生的 UI 界面供用户选择图片或视频。图片或视频的来源可以是系统相簿,也可以是相机直接拍摄。
- 使用 react-native-image-picker 我们不必关心如何与设备相册、摄像头如何交互,用户操作完毕后我们就可以直接得到资源的 URI 地址、或者 base64 字符串(限图片)。
- GitHub 主页地址:https://github.com/marcshilling/react-native-image-picker
2,安装配置
1
|
npm install react-native-image-picker @latest --save
|
1
|
react-native link
|
- Privacy - Camera Usage Description
- Privacy - Photo Library Usage Description
- Privacy - Microphone Usage Description
3,使用样例
- 点击“选择照片”按钮,界面底部出现来源选择菜单。用户可以自行选择拍照、还是从相册中选择。
- 菜单中我还添加了个自定义按钮“hangge.com图片”,点击会有响应事件。
- 选择照片选择或者拍摄完毕后,会将最终的图片显示在 Image 组件里。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
import React, { Component } from 'react' ;
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
AlertIOS
} from 'react-native' ;
//图片选择器
var ImagePicker = require( 'react-native-image-picker' );
//图片选择器参数设置
var options = {
title: '请选择图片来源' ,
cancelButtonTitle: '取消' ,
takePhotoButtonTitle: '拍照' ,
chooseFromLibraryButtonTitle: '相册图片' ,
customButtons: [
{name: 'hangge' , title: 'hangge.com图片' },
],
storageOptions: {
skipBackup: true ,
path: 'images'
}
};
//默认应用的容器组件
class App extends Component {
//构造函数
constructor(props) {
super (props);
this .state = {
avatarSource: null
};
}
//渲染
render() {
return (
<View style={styles.container}>
<Text style={styles.item} onPress={ this .choosePic.bind( this )}>选择照片</Text>
<Image source={ this .state.avatarSource} style={styles.image} />
</View>
);
}
//选择照片按钮点击
choosePic() {
ImagePicker.showImagePicker(options, (response) => {
console.log( 'Response = ' , response);
if (response.didCancel) {
console.log( '用户取消了选择!' );
}
else if (response.error) {
alert( "ImagePicker发生错误:" + response.error);
}
else if (response.customButton) {
alert( "自定义按钮点击:" + 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
});
}
});
}
}
//样式定义
const styles = StyleSheet.create({
container:{
flex: 1,
marginTop:25
},
item:{
margin:15,
height:30,
borderWidth:1,
padding:6,
borderColor: '#ddd' ,
textAlign: 'center'
},
image:{
height:198,
width:300,
alignSelf: 'center' ,
},
});
AppRegistry.registerComponent( 'ReactDemo' , () => App);
|
4,直接启动相机或访问相册
上面的样例中我们是先弹出个选择菜单,让用户决定是拍照,还是从相册中选择图片。
其实我们也可以跳过这个步骤,直接调用摄像头拍照,或者直接打开相册让用户选择照片。
1
2
3
4
5
6
7
8
9
|
//启动相机拍照
ImagePicker.launchCamera(options, (response) => {
//响应结果处理参考上面样例
});
//打开系统相册
ImagePicker.launchImageLibrary(options, (response) => {
//响应结果处理参考上面样例
});
|
原文出自: www.hangge.com 转载请保留原文链接: http://www.hangge.com/blog/cache/detail_1617.html
React Native - 使用图片选择器react-native-image-picker拍照、选照片相关推荐
- react antd-mobile v2图片选择器ImagePicker压缩图片上传腾讯云COS处理方案
本文介绍:react移动端 antd-mobile V2版本使用图片选择器 ImagePicker时,前端压缩图片并且上传腾讯云COS 如果不上传腾讯云,纯选择压缩,可查看:antd-mobile-v ...
- native react 图片多选_react-native实现的多图片选择器
react-native-syan-image-picker 功能介绍 基于已有原生第三方框架封装的多图片选择组件,适用于 React Native App. 原生框架依赖 功能特点 支持 iOS.A ...
- [RN] React Native 实现图片预览
[RN] React Native 实现图片预览 效果预览: 代码如下: 'use strict'; import React, {Component} from 'react'; import {I ...
- React Native开发之必备React基础
为了帮助大家快速上手React Native开发,在这本节中将向大家介绍开发React Native所需要的一些React必备基础知识. 概述 本节课将从React的特点.如何使用React.JSX语 ...
- mobx在react中应用_借助React Native Elements,Jest和MobX MST可以轻松实现现实世界中的ReactNative应用...
mobx在react中应用 by Qaiser Abbas 由Qaiser Abbas 借助React Native Elements,Jest和MobX MST可以轻松实现现实世界中的ReactNa ...
- 利用 Create React Native App 快速创建 React Native 应用
React Native App简介 打开React Native官方文档你会发现,在Getting Started章节下新添加一个Quick Start Tab页.Quick Start是在v0.4 ...
- react打包后图片丢失_React系列四 - React脚手架
一. 认识脚手架 1.1. 前端工程的复杂化 如果我们只是开发几个小的demo程序,那么永远不需要考虑一些复杂的问题: 比如目录结构如何组织划分: 比如如何管理文件之间的相互依赖: 比如如何管理第三方 ...
- react打包后图片丢失_手搭一个 React,Typescript,Koa,GraphQL 环境
本文系原创,转载请附带作者信息:yhlben 项目地址:https://github.com/yhlben/cdfang-spider 前言 在实际的开发过程中,从零开始初始化一个项目往往很麻烦,所以 ...
- react前端显示图片_如何在react项目中引用图片?
如何在react项目中引用图片?本文码云笔记将为大家整理在react项目中插入图片以及背景图片的方法,希望对需要的小伙伴提供一些参考. 在react项目中插入图片以及背景图片的方法共有2种: 1.im ...
最新文章
- 《评人工智能如何走向新阶段》后记(再续10)
- HDU 1874 SPFA算法Dijkstra算法
- dubbo 使用 filter 报错解决
- Spring Boot 传参方式
- 关于在ubuntu下配置AMD显卡驱动的总结
- OSPF报文详解——LSA概述
- OpenStack运维(二):OpenStack计算节点的故障和维护
- css中调整高度充满_6个很棒的PostCSS插件,让您成为一个CSS向导
- bootstrap拖动div_JS组件系列——Bootstrap Table 表格行拖拽(二:多行拖拽)
- if else语句linux,linux 中的if else语句
- crontab日常使用梳理
- Single Number III
- C语言谭浩强版本学习笔记(1)
- VS2019创建COM组件
- MongoDB从入门到高级的实战操作
- html涂鸦画板插件,sketchpad-简单的HTML5 Canvas涂鸦画板插件 -HTML5功能
- html微课制作软件,炫页网.多彩炫页工具-微课大赛指定HTML5课程制作工具
- 使用Linux搭建IPV6路由器
- 间歇性需求预测之Croston‘s method
- dva的用法_使用 dva 构建小型前端项目 (一)
热门文章
- VCS+verdi /dve 仿真环境搭建-问题汇总
- APP设计阅读:6本交互设计殿堂级的书籍
- 20221126给Chrome浏览器安装扩展程序——猫抓
- 获取input上传图片之后的文件大小,以及路径
- 乐乎常用的html源码,点点网关闭自定义html模板代码功能,网易lofter重新映入眼帘...
- 百度涉嫌行政违法;库克承诺调查“沙特用App监控女性出境”事件;苹果高管涉内幕交易遭起诉 | 雷锋早报...
- css选择器(id选择器)
- 树莓派3B+增加虚拟内存
- QtXlsx第三方库在Mac OS和Windows下的配置及简单使用
- 听李天飞《大话西游》有感