How to Setup Default Loading Image before real product image loads in Ionic applications?

Introduction

This article applies to fellow iOS10 app developers who are using the Ionic/Cordova framework.

I am going to explain the problem of setup loading images.

which I faced during the development of an ionic app using Microsoft Visual Studio and Mac OS.

Hope this will help to mobile app development community or individual Ionic app developers.

Problem

I am working in Ionic (using visual studio tools for Apache Cordova) In My Ionic application.

Suppose, I need to display a default loading image before the actual product image loads.

Because my page was not looking good until my actual product images were loaded fully in the Ionic mobile app.

program processing image gif in ios mobile app

Screenshot of default loading image

Example:

  1. When My View calls by default all the product Images are set with default loading images.
  2. Call Web services for actual data.
  3. Remain to load the image path as the default path until the new image loads fully.
  4. Fully load the product image will be replaced with the default loading image.

Solution

Let’s, understand, how to program for the setting up the default loading image or GIF file. We have to use a directive to load the actual product image when it will fully load.

Here I describe the step-by-step solution for the iOS app program.

  1. First of all, I have to set the default.gif processing image in src and set the actual product Image using the keyword new-image which is my directive.
    setup default loading image in ios app

<div class="image-box">
    <img 
        class="item-thumbnail" 
        data-rel="external" 
        src="img/default.gif" 
        new-image="{{products[$index].ImageURL}}" 
    />
</div>

  1. Now, I have to create a directive for loading images under /www/js/directive.js and Name the directive as newImage because I am using it as “new-image” in Html.
    .directive('newImage', ['$timeout', '$ionicPopup', '$compile', 'Utils', function ($timeout, $ionicPopup, $compile, Utils) {
        return {
            link: function ($scope, element, attrs) {
                function LoadNewImage(url) {
                    var Img = new Image();
                    Img.onload = function () {
                        $timeout(function () {
                            element.attr('src', url);
                        });
                    }
                    Img.src = url;
                }
                attrs.$observe('newImage', function (newValue) {
                    if (newValue) {
                        LoadNewImage(newValue);
                    }
                });
            }
        }
    }])

    You can directly copy the code for the directive.js file.

I hope this tiny help for the setup of the default processing GIF image before the product image loads will work for you. You can share this blog with your friends who are working in Ionic hybrid app development. Thanks.

Article by

Chintan Prajapati

Chintan Prajapati is the Founder and CEO of Satva Solutions and a seasoned computer engineer with over two decades of experience in the software industry. His expertise spans Accounting & ERP Integrations, Robotic Process Automation, and the development of technology solutions built around leading ERP and accounting platforms with a particular focus on responsible AI and machine learning in fintech.Chintan holds a BE in Computer Engineering and carries an impressive roster of certifications, including Microsoft Certified Professional, Microsoft Certified Technology Specialist, Certified Azure Solution Developer, Certified Intuit Developer, Certified QuickBooks ProAdvisor, and Xero Developer.Over the course of his career, he has made a measurable impact on the accounting industry consulting on and delivering integration and automation solutions that have collectively saved thousands of man-hours. His writing aims to offer readers practical, insight-driven advice on harnessing technology to unlock greater business efficiency.When he steps away from the desk, Chintan can be found trekking through mountain trails or watching birds in the wild. Grounded in the philosophy of delivering the highest value to clients, he continues to champion innovation and excellence in digital transformation from his home base in Ahmedabad, India.