native react 图片多选_react-native-image-picker 集成,使用(选择,删除)

本文介绍了如何在 React Native 中集成 react-native-image-picker 库,实现图片多选功能。步骤包括安装依赖、配置安卓权限、修改 MainApplication.java 文件、设置选择选项、展示选择图片以及删除图片的操作。此外,还提到了上传图片可以使用 axios 等库。
摘要由CSDN通过智能技术生成

1.安装依赖

yarn add react-native-image-picker

react-native link

2.适配安卓

①. /android/app/src/main/AndroidManifest.xml文件中添加权限

include ':react-native-image-picker'

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

③ android/app/build.gradle文件的dependencies中添加

compile project(':react-native-image-picker')

④ /android/app/src/main/java/com/newredsj/MainApplication.java文件中导入包,方法

import com.imagepicker.ImagePickerPackage;

new ImagePickerPackage() //在getPackages方法添加

3.使用

① 设置option

const options = {

title: '选择图片',

cancelButtonTitle: '取消',

takePhotoButtonTitle: '拍照',

chooseFromLibraryButtonTitle: '图片库',

...........,

storageOptions: {

skipBackup: true,

path: 'images',

},

};

② 选择图片fn

showImagePicker () {

ImagePicker.showImagePicker (options, response => {

console.log(response);

if (response.didCancel) {

console.log ('用户点击了取消');

} else if (response.error) {

console.log ('ImagePicker 出错: ', response.error);

} else {

let source;

if (Config.isAndroid) {

source = {uri: response.uri, isStatic: true};

} else {

source = {uri: response.uri.replace ('file://', ''), isStatic: true};

}

let photobase = 'data:image/jpeg;base64,'+ response.data (需要base64上传得时候添加)

let imageArray = this.state.avatarSource;

let fileArray = this.state.upfileData;

imageArray.push (source);

fileArray.push(photobase)

this.setState ({

avatarSource: imageArray,

upfileData:fileArray

});

this.props._selectPhotoFun(this.state.upfileData)

}

});

}

③ 选择图片得renderView

_renderAddImage(){

return (

source={require ('../Image/Common/line.png')}

style={styles.image}

>

上传图片

)

}

④ 操作及展示renderView

_renderView = () => {

let arr = [];

if(this.state.avatarSource!=''){

for (let index = 0; index < this.state.avatarSource.length; index++) {

arr.push(

source={this.state.avatarSource[index]}

style={styles.imageL}

imageStyle={{resizeMode: 'cover'}}

key={index}

>

onPress={this.deleteLoadedImage.bind (this, index)}

style={styles.rightDelButton}

>

)

}

return arr;

}

}

⑤ 删除就是简单得数组操作就不上代码了

⑥ 至于上传可用 fetch,axios 等。个人推荐npm:axios

最后效果图

其实开发过程中麻烦得也就是布局,其他操作看个人喜好,以及业务需求!

学习使人快乐,代码改变人生,新手上路,各路大神,不喜勿碰!求反馈!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值