ImagePicker 图片选择器

注意:只是图片选择器,一般用于上传图片前的文件选择操作,但不包括图片上传的功能. 选择器中使用了Promise,请参考Promise中兼容性的说明. 如果必要,可以参考/docs/react/introduce-cn进行兼容性处理.

代码演示

简单的图片选择组件

  1. import { ImagePicker, WingBlank, SegmentedControl } from 'antd-mobile';
  2. const data = [{
  3. url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg',
  4. id: '2121',
  5. }, {
  6. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg',
  7. id: '2122',
  8. }];
  9. class ImagePickerExample extends React.Component {
  10. state = {
  11. files: data,
  12. multiple: false,
  13. }
  14. onChange = (files, type, index) => {
  15. console.log(files, type, index);
  16. this.setState({
  17. files,
  18. });
  19. }
  20. onSegChange = (e) => {
  21. const index = e.nativeEvent.selectedSegmentIndex;
  22. this.setState({
  23. multiple: index === 1,
  24. });
  25. }
  26. render() {
  27. const { files } = this.state;
  28. return (
  29. <WingBlank>
  30. <SegmentedControl
  31. values={['切换到单选', '切换到多选']}
  32. selectedIndex={this.state.multiple ? 1 : 0}
  33. onChange={this.onSegChange}
  34. />
  35. <ImagePicker
  36. files={files}
  37. onChange={this.onChange}
  38. onImageClick={(index, fs) => console.log(index, fs)}
  39. selectable={files.length < 7}
  40. multiple={this.state.multiple}
  41. />
  42. </WingBlank>
  43. );
  44. }
  45. }
  46. ReactDOM.render(<ImagePickerExample />, mountNode);

自定义选择图片的方法

  1. import { ImagePicker } from 'antd-mobile';
  2. const data = [{
  3. url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg',
  4. id: '2121',
  5. }, {
  6. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg',
  7. id: '2122',
  8. }];
  9. class ImagePickerExample extends React.Component {
  10. state = {
  11. files: data,
  12. };
  13. onChange = (files, type, index) => {
  14. console.log(files, type, index);
  15. this.setState({
  16. files,
  17. });
  18. };
  19. onAddImageClick = (e) => {
  20. e.preventDefault();
  21. this.setState({
  22. files: this.state.files.concat({
  23. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg',
  24. id: '3',
  25. }),
  26. });
  27. };
  28. onTabChange = (key) => {
  29. console.log(key);
  30. };
  31. render() {
  32. const { files } = this.state;
  33. return (
  34. <div>
  35. <ImagePicker
  36. files={files}
  37. onChange={this.onChange}
  38. onImageClick={(index, fs) => console.log(index, fs)}
  39. selectable={files.length < 5}
  40. onAddImageClick={this.onAddImageClick}
  41. />
  42. </div>
  43. );
  44. }
  45. }
  46. ReactDOM.render(<ImagePickerExample />, mountNode);

自定义文件类型

  1. import { ImagePicker } from 'antd-mobile';
  2. const data = [{
  3. url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg',
  4. id: '2121',
  5. }, {
  6. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg',
  7. id: '2122',
  8. }];
  9. class ImagePickerExample extends React.Component {
  10. state = {
  11. files: data,
  12. }
  13. onChange = (files, type, index) => {
  14. console.log(files, type, index);
  15. this.setState({
  16. files,
  17. });
  18. }
  19. render() {
  20. const { files } = this.state;
  21. return (
  22. <div>
  23. <ImagePicker
  24. files={files}
  25. onChange={this.onChange}
  26. onImageClick={(index, fs) => console.log(index, fs)}
  27. selectable={files.length < 5}
  28. accept="image/gif,image/jpeg,image/jpg,image/png"
  29. />
  30. </div>
  31. );
  32. }
  33. }
  34. ReactDOM.render(<ImagePickerExample />, mountNode);

自定义数量

  1. import { ImagePicker } from 'antd-mobile';
  2. const data = [{
  3. url: 'https://zos.alipayobjects.com/rmsportal/PZUUCKTRIHWiZSY.jpeg',
  4. id: '2121',
  5. }, {
  6. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg',
  7. id: '2122',
  8. }];
  9. class ImagePickerExample extends React.Component {
  10. state = {
  11. files: data,
  12. };
  13. onChange = (files, type, index) => {
  14. console.log(files, type, index);
  15. this.setState({
  16. files,
  17. });
  18. };
  19. onAddImageClick = (e) => {
  20. e.preventDefault();
  21. this.setState({
  22. files: this.state.files.concat({
  23. url: 'https://zos.alipayobjects.com/rmsportal/hqQWgTXdrlmVVYi.jpeg',
  24. id: '3',
  25. }),
  26. });
  27. };
  28. onTabChange = (key) => {
  29. console.log(key);
  30. };
  31. render() {
  32. const { files } = this.state;
  33. return (
  34. <div>
  35. <ImagePicker
  36. length="6"
  37. files={files}
  38. onChange={this.onChange}
  39. onImageClick={(index, fs) => console.log(index, fs)}
  40. selectable={files.length < 7}
  41. onAddImageClick={this.onAddImageClick}
  42. disableDelete
  43. />
  44. </div>
  45. );
  46. }
  47. }
  48. ReactDOM.render(<ImagePickerExample />, mountNode);

ImagePicker图片选择器 - 图1

API

属性说明类型默认值
files图片文件数组,元素为对象,包含属性 url(必选, 可能还有id, orientation, 以及业务需要的其它属性Array[]
onChangefiles 值发生变化触发的回调函数, operationType 操作类型有添加,移除,如果是移除操作,则第三个参数代表的是移除图片的索引(files: Object, operationType: string, index: number): void
onImageClick点击图片触发的回调(index: number, files: Object): void
onAddImageClick自定义选择图片的方法(): void
onFail选择失败(msg: string): void
selectable是否显示添加按钮booleantrue
multiple是否支持多选booleanfalse
accept图片类型stringimage/*
length单行的图片数量string | number4
capture图片捕获设置, 具体请参考MDN中关于capture的说明boolean | stringfalse
disableDelete是否隐藏删除按钮booleanfalse