react-native-photo-view-ex

Displaying photos with pinch-to-zoom, based on react-native-photo-view

Usage no npm install needed!

<script type="module">
  import reactNativePhotoViewEx from 'https://cdn.skypack.dev/react-native-photo-view-ex';
</script>

README

react-native-photo-view-ex

License npm Version

Provides custom Image view for React Native that allows to perform pinch-to-zoom on images. Works on both iOS and Android.

This component uses PhotoDraweeView for Android and MWPhotobrowser on iOS.

About this fork

This fork is based on the great work of @alwx (Alexander Pantyuhov) and has this changes:

  • Android Build Tools 28.0.3 and targetSdkVersion 28 (configurable).
  • Peer dependent on react-native v0.57 and above.
  • Using the same version of fresco included by react-native.
  • Updated dependencies
  • Changes to gradle config to support the new version.
  • Changes to some properties, mainly androidScaleType replaced by resizeMode.
  • PhotoView as PureComponent.
  • Minor fixes.
  • Typescript definitions.

All this is to adapt it better to my current project (it is not tested on iOS), but feel free to use it.

Usage

import PhotoView from 'react-native-photo-view-ex';

Basics:

  <PhotoView
    source={{uri: 'https://facebook.github.io/react/img/logo_og.png'}}
    minimumZoomScale={0.5}
    maximumZoomScale={3}
    resizeMode="center"
    onLoad={() => console.log("Image loaded!")}
    style={{width: 300, height: 300}}
  />

Properties

Property Type Description
source Object The same as source for other React images, except that it does not handle arrays.
loadingIndicatorSource Object Similarly to source, but used to render the loading indicator. Must be a local image
fadeDuration int Duration of the fade, in milliseconds.
scale float Zoom scale
maximumZoomScale float The maximum allowed zoom scale. The default value is 3.0
minimumZoomScale float The minimum allowed zoom scale. The default value is 1.0
resizeMode String Android only: One of "center", "contain", "cover", "fitStart", "fitEnd", "stretch". The default is "cover"
style Array or Object Subset of react-native style. See index.d.ts
showsHorizontalScrollIndicator bool iOS only: When true, shows a horizontal scroll indicator. The default value is true.
showsVerticalScrollIndicator bool iOS only: When true, shows a vertical scroll indicator. The default value is true.
zoomTransitionDuration int Android only: Double-tap zoom transition duration, in milliseconds.

Events

Name Parameter * Description
onError - Invoked on load error.
onLayout layout: {x, y, width, height} Invoked on mount and layout changes.
onLoad - Invoked when load completes successfully.
onLoadEnd - Invoked when load either succeeds or fails.
onLoadStart - Invoked on load start.
onProgress loaded, total iOS only: Invoked on download progress.
onScale scale, scaleFactor, focusX, focusY Callback function called on scale changes.
onTap scale, x, y Callback function called on image tap.
onViewTap scale, x, y Callback function called on tap outside of the image.

* In the nativeEvent property.

Compared to react-native-image-zoom

react-native-image-zoom functionality is similar, but there are several major differencies:

  • PhotoView is based on PhotoDraweeView which is the "PhotoView For Fresco". It works better, it supports several important callbacks out-of-box and it is, actually, recommended by Chris Banes, because his PhotoView (base for react-native-image-zoom) doesn't completely support Facebook Fresco.
  • PhotoView has more options like fadeDuration and minimumZoomScale/maximumZoomScale and more important callbacks.
  • PhotoView is written in the same manner as default React Image, and it supports most of the features Image has (the goal is to be fully compaitable with Image and support absolutely everything).
  • It is possible to use PhotoView as a container (currently iOS only)!

Automatic installation

Just two simple steps:

yarn add react-native-photo-view-ex
react-native link react-native-photo-view-ex

Manual installation

Android

  1. Add these lines to android/settings.gradle

    include ':react-native-photo-view-ex'
    project(':react-native-photo-view-ex').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-photo-view-ex/android')
    
  2. Add the dependency to android/app/build.gradle

    dependencies {
      implementation project(':react-native-photo-view-ex')
    }
    
  3. Register PhotoViewPackage in your MainApplication.java:

    import io.amarcruz.photoview.PhotoViewPackage;
    
    // ...
    
    public class MainActivity extends ReactActivity {
      // ...
    
      @Override
      protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new PhotoViewPackage() // add this manager
        );
      }
    
      // ...
    }
    

iOS

  1. Add this line to your podfile

    pod 'react-native-photo-view-ex', path: './node_modules/react-native-photo-view-ex'
    
  2. Run pod install