Building an Ionic Image Gallery With Zoom

ionic-image-gallery-header

The image gallery is a classic pattern used in many applications which need to display any kind of grid with pictures or photos to the user. If you have specific requirements, there might not be an out of the box solution that works fine for you, that’s why we will create our own Ionic image gallery.

The Ionic Academy

More content, more detailed video courses and community help inside the member area.

Not an Ionic Academy Member? Click here to learn more

We will display images in a gallery like infinite loop where we can always see the end of the previous image and the start of the following. This question came up inside the forum and the solution we found should do the trick. Additional we make use of the Ionic-img-viewer component which helps us to display a selected picture in fullscreen with zooming options.

I tried to implement zooming on my own, but at the time writing this article (v3.0.1) the ion-scroll property for zooming does not really work. Hopefully in the future we will be able to use that zoom to build our own zooming view!

Setting up our Image Gallery App

For the start we need only a blank Ionic app and install already the npm package for our image preview, so go ahead and run:

Before we do the rest of our work, we need to connect everything inside our src/app/app.module.ts to load the module we have just installed:

Very basic, nothing really special at this time!

Building our Image Gallery

If you want to display some images, make sure to add some to your project. In my case I copied 4 images to the src/assets/img folder, named “1.jpg” and so on.

To load these images we create a very simple array inside our page, so go ahead and add it to the src/pages/home/home.ts:

Next the actual view for the image gallery. We can make use of the ion-slides component, which is normally displaying full pages of content where you can swipe through.

However, with some properties and CSS we can finally achieve the desired look for our gallery!

Also, this component is based on the official Swiper component, where you can find many examples for animation of this component, including different effects and how the properties change the behaviour.

Almost all of these work with the Ionic component as well, so if you want some cool effects for example try adding effect="coverflow" for a nice coverflow effect with your Ionic slides!

Back to our view, we loop through our images, create the slides and display the according image in each of them. There we also add the imageViewer property which will take care of the click event on an image and display the special image viewer!

Now go ahead and insert to your src/pagges/home/home.html:

As said before this is not yet the desired outcome, therefore we need to apply a tiny CSS tweak and make it look a bit better as well. Put the code below into the src/pages/home/home.scss:

Now your image slider should do exactly as we want, and the result will look like the images below. First, the gallery inside your view:

By using 2 slides per view but adding the distance to the left and setting the size of each slide to only 50% we were able to craft this tricky view.

If you now select one image, the image viewer component comes up where you can zoom in and swipe down to leave the view (or use the button at the top):

Conclusion

It actually don’t takes a lot of code to build a gallery, but if you want it in a special way you might have to tinker around with some CSS that works for you. Also, using an external module for the zooming view is not really perfect (although many thanks to Riron for creating it!), but hopefully this will be fixed in an upcoming Ionic release.

For now enjoy your Ionic image gallery with zooming functions!

You can watch the video version of this article below.

Happy Coding,
Simon

  • negrelis

    Runtime Error
    Cannot find module “ionic-angular/gestures/pan-gesture”

    • gunjankrs

      I have fixed this issue by correcting right gesture file. I have raised a pull request for it. If you want to fix it quickly change it to “ionic-angular/gestures/drag-gesture”

      • Henrique Jacob Rech

        I’ve tried these changes but had no success on making it work. =/

  • Sachin Mishra

    The problem is with respect to version difference of the ionic 3 and ionic2.
    You may sort out the by removing the reference of the “ionic-angular/gestures/pan-gesture” to
    “ionic-angular”

    I hope this may help you !!!!!