使用react-native-image-viewer實現(xiàn)大圖預覽
react-native-image-viewer大圖預覽
在移動開發(fā)中,特別是涉及到圖片的應(yīng)用開發(fā)中,經(jīng)常會遇到圖片預覽等功能,并且預覽支持圖片的放大和縮小,在Android原生開發(fā)中可以使用PhotoViewPager庫實現(xiàn),如果在React Native中,可以選擇使用react-native-image-viewer。
先看一個實現(xiàn)的效果
實例
使用前需要先安裝react-native-image-viewer庫,安裝命令如下:
npm i react-native-image-zoom-viewer --save
然后引入ImageViewer組件,然后設(shè)置圖片的數(shù)據(jù)源即可,imageUrls接受一個圖片數(shù)組。
下面是一個簡單的實例代碼
import React, {PureComponent, Component} from 'react' import {View, Modal,Navigator} from 'react-native'; import ImageViewer from 'react-native-image-zoom-viewer'; const images = [{ url: "https://avatars2.gitdubusercontent.com/u/7970947?v=3&s=460", // url: // "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1527660246058&di=6f0f1b19cf05a64317cbc5d2b3713d64&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F0112a85874bd24a801219c7729e77d.jpg", // props: { // // headers: ... // // source: require('./img.png') // }, // freeHeight: true }, {url: "https://avatars2.gitdubusercontent.com/u/7970947?v=3&s=460"} ]; export default class ImageZoom extends PureComponent { state = { index: 0, modalVisible: true }; render() { return ( <View style={{ padding: 10 }} > <Modal visible={tdis.state.modalVisible} transparent={true} onRequestClose={() => tdis.setState({modalVisible: false})}> <ImageViewer imageUrls={images} index={tdis.state.index}/> </Modal> </View> ) } }
Props
react-native-image-viewer庫提供了諸多的屬性,常見的屬性如下。
屬性名 | type | 描述 | 默認值 |
imageUrls(必填) | array | 圖片數(shù)據(jù)源 | |
enableImageZoom | boolean | 是否可以進行縮放操作 | true |
onShowModal | (content?: JSX.Element) => void | 彈出大圖回調(diào) | () => {} |
onCancel | () => void | 取消圖片回調(diào) | () => {} |
fliptdreshold | number | 滑動到下一頁的X值 | 80 |
maxOverflow | number | 滑動到下一頁的X位置最大值 | 300 |
index | number | 初始顯示第幾張圖片 | 0 |
failImageSource | {uri: string} | 加載失敗顯示圖片 | ” |
loadingRender | () => React.ReactElement | 渲染loading | () => null |
onSaveToCamera | (index?: number => void | 保存到相機的回調(diào) | () => {} |
onChange | (index?: number => void | 圖片切換時觸發(fā) | () => {} |
saveToLocalByLongPress | boolean | 是否開啟長按保存圖片到本地 | true |
onClick | (onCancel?: function) => void | 單擊事件 | (onCancel) => {onCancel()} |
onDoubleClick | (onCancel?: function) => void | 雙擊事件 | (onCancel) => {onCancel()} |
onSave | (url: string) => void | 保存圖片到本地, | (onCancel) => {onCancel()} |
renderFooter | () => React.ReactElement | 自定義頭部 | () => null |
renderFooter | () => React.ReactElement | 自定義尾部視圖 | () => null |
renderIndicator | (currentIndex?: number, allSize?) => React.ReactElement: number | 自定義計時器 | (currentIndex, allSize) => currentIndex + “/” + allSize |
renderImage | (props: any) => React.ReactElement | 自定義Image組件 | (props) => |
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
通過React-Native實現(xiàn)自定義橫向滑動進度條的 ScrollView組件
開發(fā)一個首頁擺放菜單入口的ScrollView可滑動組件,允許自定義橫向滑動進度條,且內(nèi)部渲染的菜單內(nèi)容支持自定義展示的行數(shù)和列數(shù),在內(nèi)容超出屏幕后,渲染順序為縱向由上至下依次排列,對React Native橫向滑動進度條相關(guān)知識感興趣的朋友一起看看吧2024-02-02React組件內(nèi)事件傳參實現(xiàn)tab切換的示例代碼
本篇文章主要介紹了React組件內(nèi)事件傳參實現(xiàn)tab切換的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-07-07React?Native?中處理?Android?手機吞字的解決方案
這篇文章主要介紹了React?Native?中處理?Android?手機吞字的解決方案,作者在 React Native 0.67.4 環(huán)境下,編寫了一個小 demo 來復現(xiàn)這個問題,需要的朋友可以參考下2022-08-08React項目中fetch實現(xiàn)跨域接收傳遞session的解決方案
這篇文章主要介紹了React項目中fetch實現(xiàn)跨域接收傳遞session的解決方案,本次項目使用了react框架,同時使用fetch取代ajax作為獲取接口數(shù)據(jù)的交互方法,下面就對這次問題的解決做個總結(jié),需要的朋友可以參考下2022-04-04用react-redux實現(xiàn)react組件之間數(shù)據(jù)共享的方法
這篇文章主要介紹了用react-redux實現(xiàn)react組件之間數(shù)據(jù)共享的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06