react-native 图片预览组件

上图

在这里插入图片描述
此组件基于通用弹窗组件,将完整代码复制到项目即可:https://blog.csdn.net/qq_38545425/article/details/121922005

安装依赖:yarn add react-native-image-zoom-viewer

  • 采用函数式调用,一行代码即可唤起预览效果,可预览本地及网络图片
上源码
//ImageZoom.js
import React, {PureComponent} from 'react';
import {Modal} from 'react-native';
import ImageViewer from 'react-native-image-zoom-viewer';
import {showModal} from '../showModal/ShowModal';
import PropTypes from 'prop-types';

/**
 * 预览图片集合
 * @param imageUrls  图片地址
 * @param index 当前下标
 */
export const previewImgArray = (imageUrls = [], index = 0) => {
  let newArr = imageUrls.map((item) => {
    if (item.indexOf('http://') !== -1 || item.indexOf('https://') !== -1) {
      return {url: item};
    } else {
      return {url: '', props: {source: item}};
    }
  });
  showModal(<ImageZoom
    imageUrls={newArr}
    index={index}
  />);
};

/**
 * 预览单张图片
 * @param imageUrl
 */
export const previewImg = (imageUrl) => {
  let imageUrls = [{
    url: imageUrl
  }];
  showModal(<ImageZoom
    imageUrls={imageUrls}
    index={0}
  />);
};

class ImageZoom extends PureComponent {

  constructor(props) {
    super(props);
    this.state = ({
      modalVisible: true
    });
  }

  static propTypes = {
    imageUrls: PropTypes.array.isRequired,
    index: PropTypes.number
  };

  hidden = () => {
    this.setState({
      modalVisible: false
    });
  };

  render() {
    return <Modal
      visible={this.state.modalVisible}
      transparent={true}
      animationType={'fade'}
      onRequestClose={() => this.setState({modalVisible: false})}>
      <ImageViewer
        onClick={() => {
          this.hidden();
        }}
        imageUrls={this.props.imageUrls}
        index={this.props.index}
      />
    </Modal>;
  }
}
使用示例
previewImgArray( [
     'https://pics5.baidu.com/feed/0824ab18972bd407b1c6e9fac20d0b580eb309c5.jpeg?token=a1a3cee62368dd3200b17a880a41bb53',
     'https://pics4.baidu.com/feed/d52a2834349b033b28274266d74aa3dad439bd69.png?token=833e80ac83dc15945f896b51049b7045',
     'https://pics1.baidu.com/feed/472309f79052982226519ab1684eeec20b46d4f4.jpeg?token=30ae1c7548f276a6c250fbee8dd808f5'
], 1)

原文:https://blog.csdn.net/qq_38545425/article/details/121991737

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值