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.百度.com/feed/0824ab18972bd407b1c6e9fac20d0b580eb309c5.jpeg?token=a1a3cee62368dd3200b17a880a41bb53',
'https://pics4.百度.com/feed/d52a2834349b033b28274266d74aa3dad439bd69.png?token=833e80ac83dc15945f896b51049b7045',
'https://pics1.百度.com/feed/472309f79052982226519ab1684eeec20b46d4f4.jpeg?token=30ae1c7548f276a6c250fbee8dd808f5'
], 1)
原文:https://blog.csdn.net/qq_38545425/article/details/121991737
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanhfhbhhb
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
TikTok加速器哪个好免费的TK加速器推荐
TK小达人 10-01 -
怎样阻止微信小程序自动打开
PHP中文网 06-13