探索 React Native 的图像处理利器:`react-native-image-crop-picker`
warning:
这篇文章距离上次修改已过203天,其中的内容可能已经有所变动。
import React from 'react';
import { View, Button, Image } from 'react-native';
import ImageCropPicker from 'react-native-image-crop-picker';
export default class ImagePickerExample extends React.Component {
pickImage = () => {
ImageCropPicker.openPicker({
width: 300,
height: 400,
multiple: true,
}).then(images => {
console.log(images);
// 处理选中的图片,例如展示在界面上
this.setState({
image: images[0].path // 假设我们只选择了一张图片
});
}).catch(error => {
console.log(error);
});
}
render() {
return (
<View>
<Button title="选择图片" onPress={this.pickImage} />
{this.state.image && <Image source={{ uri: this.state.image }} style={{width: 300, height: 400}} />}
</View>
);
}
}
这段代码展示了如何在React Native应用中使用react-native-image-crop-picker
库来选择并显示图片。按钮点击时触发pickImage
函数,该函数调用图片选择器,用户选择图片后,图片的路径被设置到组件的状态中,随后在界面上以Image组件的形式展示出来。
评论已关闭