Images displayed in your app may be responsible for a large portion of the bandwidth consumed by the device. This has a direct impact on the app’s performance, battery consumption, and the amount of memory the app allocates. As a result, optimizing images can often bring noticeable performance improvements for your app: the fewer bytes it needs to download, the smaller the impact is on the client’s bandwidth and the faster app will download and render content on the screen.
Let’s imagine you have an app where you store pictures to show them to your app’s users. What happens if the resolution of these images is high and they are taking a lot of space? Downloading these files is time-consuming and, as a result, slows down your app. The result is a subpar user experience.
One recommended solution is to create image thumbnails with lower resolutions relative to the original one. These thumbnails can be used to preview the image in the application.
The thumbnails can be generated using Backendless API Services (in the Business Logic (Cloud Code) section). If you are not familiar with how to create your own API Service, please check the How to generate a QR code with Backendless API Service post, which describes the process of API service creation in greater detail.
In this article, we will focus on the task of generating thumbnail images with different resolutions.
Below is the source code for the service that performs the task:
We would also need at least one image file for testing. So, let’s go to the Files section and upload a test picture into the images directory:
Next, we’ll go to the Data section and create the images table with two fields:
column name: name, data type STRING
column name: original, data type FILE REFERENCE
Switch to the DATA BROWSER section and create a new object as shown below. The original column must contain a file reference to the file for which the thumbnails will be generated by the service:
Now run CodeRunner in debug mode and try your service in action using the API Services section of the Business Logic screen:
The imageId argument is an objectId of the record in the images table.
And here is the result:
As you can see, the newly created images have different file sizes. You can use any of those thumbnail types for the preview purposes in your app.