简体   繁体   English

带有缩放和滑动器的 React-Native 图像查看器

[英]React-Native image viewer with zoom and swiper

I have an array of images in a Swiper, where I can swipe through them, and when I click in on of them it open on a Modal (using Lightbox).我在 Swiper 中有一组图像,我可以在其中滑动它们,当我点击它们时,它会在模态(使用灯箱)上打开。 But Lightbox doesn't have Pinch-to-Zoom or swiping.但 Lightbox 没有双指缩放或滑动功能。

So im looking for a solution to this.所以我正在寻找解决方案。 I already have a swiper, but when I open an image, I want to still be able to swipe through all the images (just like Facebook, you can view all the photos, or open one and swipe through them).我已经有一个 swiper,但是当我打开一个图像时,我希望仍然能够滑动浏览所有图像(就像 Facebook 一样,您可以查看所有照片,或者打开一张并滑动浏览它们)。 In addition to this I need to be able to Pinch-to-Zoom.除此之外,我还需要能够捏合缩放。

Right now this is my code:现在这是我的代码:

(Relevant code) (相关代码)

      <Swiper
        styles={{flex: 1}}
        height={250}
        renderPagination={this.renderPagination}
        paginationStyle={{
          bottom: -23, left: null, right: 10
        }} loop={false}>
          {this.state.imagens.map((item, index) => {
            return(
              <NetworkImage
                source={{uri: `URL/${item.Ficheiro}`, height:250, width: Dimensions.get('window').width}}>
                <Lightbox navigator={this.props.navigator}>
                  <Image
                    style={{ height: 300 }}
                    source={{ uri: `URL/${item.Ficheiro}` }}
                  />
                </Lightbox>
              </NetworkImage>
            );
          })}
      </Swiper>

Im using this library for the swiper https://github.com/leecade/react-native-swiper and I saw it has a PhotoView, but I couldn't get it to work.我将这个库用于 swiper https://github.com/leecade/react-native-swiper ,我看到它有一个 PhotoView,但我无法让它工作。

I've been trying to implement something similar to this as well.我也一直在尝试实现类似的东西。

I'm using react-native-image-zoom-viewer for the zoomed in mode after clicking one of the pictures in the swiper.单击 swiper 中的一张图片后,我将react-native-image-zoom-viewer用于放大模式。 Within the modal, you can zoom in and out an image while swiping between images.在模态中,您可以在图像之间滑动时放大和缩小图像。

https://www.npmjs.com/package/react-native-image-zoom-viewerhttps://www.npmjs.com/package/react-native-image-zoom-viewer

I haven't fully implemented the solution yet but it seems you can just wrap the ImageViewer component in any Modal that you can open/close it programmatically.我还没有完全实现该解决方案,但似乎您可以将 ImageViewer 组件包装在任何可以以编程方式打开/关闭它的 Modal 中。

<Modal visible={this.state.isModalOpened} transparent={true}>
   <ImageViewer imageUrls={images} index={this.state.currentImageIndex}/>
</Modal>

And with the modal somewhere sitting in your page, for the Swiper you can map over your images and return clickable images as follows:使用位于页面中某处的模态,对于 Swiper,您可以映射图像并返回可点击的图像,如下所示:

<View style={styles.slide} key={index}>
   <TouchableWithoutFeedback onPress={() => {this.openModal(index)}}>
     <Image
       resizeMode="cover"
       style={styles.image}
       source={{ uri: img.url }}
     />
   </TouchableWithoutFeedback>
</View>

As seen above, each image is wrapped by an onPress that opens the modal according to the image index, so it opens the ImageViewer Modal on the right photo.如上所示,每个图像都被一个 onPress 包裹,它根据图像索引打开模态,因此它会在右侧照片上打开 ImageViewer 模态。

And openModal should look something like this: openModal 应该是这样的:

function openModal(index) {
   this.setState({isModalOpened: true, currentImageIndex: index })
}

And the state should be:状态应该是:

this.state={
  isModalOpened: false,  //Controls if modal is opened or closed
  currentImageIndex: 0   //Controls initial photo to show for modal
}

I'm using react-native-image-zoom-viewer with hooks https://www.npmjs.com/package/react-native-image-zoom-viewer我正在使用带有钩子的react-native-image-zoom-viewer https://www.npmjs.com/package/react-native-image-zoom-viewer

import React, { useState } from 'react';
import ImageViewer from 'react-native-image-zoom-viewer';

const MultipleImageFeaturePost = ({ route, navigation }) => {
            
     **const { item } = route.params
    const [showModal, setshowModal] = useState(false)
    const [imageIndex, setimageIndex] = useState(0)
    const images = item.post_medias.map(s => ({ url: s.media_name })) **
            
     const renderImages = (item, index) => (
        <TouchableOpacity onPress={() => {
     ** setimageIndex(index)
        setshowModal(true) **
     }}>
            <Image
                source={{ uri: item.media_name }}
                resizeMode='stretch'
                style={{ alignSelf: 'center', height: hp('35'), marginVertical: hp('1%'), width: wp('86%'), borderRadius: 7, backgroundColor: '#A9A9A9' }} />
        </TouchableOpacity>
    )
    return (
        <SafeAreaView style={{ flex: 1, backgroundColor: '#E5EBEE' }}>
            <FlatList
                showsHorizontalScrollIndicator={false}
                data={item.post_medias}
                renderItem={({ item, index }) => renderImages(item, index)}
                keyExtractor={(item) => item.post_media_id + ''}
            />
         
        ** <Modal
            visible={showModal}
            transparent={true}
            onSwipeDown={() => setshowModal(false)}>
            <ImageViewer
                imageUrls={images}
                index={imageIndex}
                onSwipeDown={() => setshowModal(false)}
                // onMove={data => console.log(data)}
                enableSwipeDown={true}/>
        </Modal> **
     </SafeAreaView >
     )
     }

export default MultipleImageFeaturePost

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM