description |
---|
Real-time image resizing, automatic optimization, and file uploading in Android using ImageKit.io. |
This is a quick start guide to show you how to integrate ImageKit in an iOS application. The code samples covered here are hosted on Github: https://github.com/imagekit-samples/quickstart/tree/master/ios.
This guide walks you through the following topics:
- Clone and run the tutorial app
- Setting up ImageKit iOS SDK
- Rendering images
- Applying common image manipulations
- Adding overlays to images
- Client-side file uploading
For this tutorial, it is recommended to use the sample iOS app, as shown below. If you already have an existing iOS app, it is also possible to use that, which will be explained in the continuation of the sample app.
git clone
https://github.com/imagekit-samples/quickstart.git
Navigate to the cloned repository, and install the dependencies that are needed to run the iOS app:
cd quickstart/ios/
pod install
{% hint style="warning" %} If you do not have CocoaPods Installed on your Mac, you can learn about setting up the development environment here https://guides.cocoapods.org/using/getting-started.html. {% endhint %}
Once the dependencies have been installed, run the following command to open the Xcode Workspace:
open ImagekitDemo.xcodeworkspace
Select the emulator from the dropdown. Then run the app by clicking on the Run button or by pressing ⌘ + R
You should see the following screen. This means the sample app has been set up correctly.
We will be installing ImageKit iOS SDK using CocoaPods.
If you are not using CocoaPods in your project, you might need to add it to your project by either running pod init
or adding PodFile (see example) in the root directory of your project.
To Install the SDK, add the following to the PodFile.
# Required to enable the use for Dynamic Libraries
use_frameworks!
target 'target_name' do
...
# Add to your project target
pod 'ImageKitIO', '~> 2.0.0'
...
end
Open AppDelegate.swift
file, this is where we will initialize our SDK with the following parameters.
urlEndpoint
is the required parameter. You can get the value of URL-endpoint from your ImageKit dashboard - https://imagekit.io/dashboard/url-endpoints.publicKey
andauthenticationEndpoint
parameters are optional and only needed if you want to use the SDK for client-side file upload. You can get these parameters from the developer section in your ImageKit dashboard - https://imagekit.io/dashboard/developer/api-keys.
ImageKit.init(
publicKey: "your_public_api_key=",
urlEndpoint: "https://ik.imagekit.io/your_imagekit_id",
transformationPosition: TransformationPosition.PATH,
authenticationEndpoint: "http://www.yourserver.com/auth"
)
Image URL can be created from an image path or using the absolute image URL. You can learn more about it in docs.
To render an image using an absolute URL (full image URL), we can instantiate the ImageKitURLConstructor
let urlConstructor = ImageKit.shared.url(
src: "https://ik.imagekit.io/demo/default-image.jpg"
)
To create a URL from the image path, we can instantiate the ImageKitURLConstructor
which takes the URL endpoint, image path, and transformation position as parameters to create the transformed url.
let urlConstructor = ImageKit.shared.url(
urlEndpoint: "https://ik.imagekit.io/demo"
path: "default-image.jpg",
transformationPosition: TransformationPosition.QUERY
)
{% hint style="info" %} The transformation position (path or query) is only valid when creating a URL from the image path. Transformations are always added as query parameters if the URL is created from an absolute image path using src. {% endhint %}
The iOS SDK provides a function for each transformation parameter, making the code simpler and readable. To add transformations, the functions can be chained with urlConstructor
.
urlConstructor = urlConstructor.height(height: 400)
urlConstructor = urlConstructor.aspectRatio(width: 3, height: 2)
Finally, once all the required transformations are defined, call create
function to get the final transformed URL
let url = urlConstructor.create()
// https://ik.imagekit.io/your_imagekit_id/default-image.jpg?tr=h-400,ar-3-2
It will look as shown below. In the sample app, the buttons are present to demonstrate the use of different transformations. \
This section covers the basics:
- Basic image resizing
- Crop Mode
- Aspect Ratio
- Chained transformation
ImageKit iOS SDK provides a function to each transformation parameter e.g. height
for h
and width
for w
parameter. It makes your code more readable. See the full list of supported transformations in iOS SDK on Github.
The complete list of transformations supported and their usage in ImageKit can be found here. If a transformation is supported in ImageKit, but if a name for it cannot be found in the SDK, then use the addCustomTransformation
function and pass the transformation code from ImageKit docs as the first parameter and value as the second parameter. For example - .addCustomTransformation("w", "400").
Let's resize the image to a height of 150 and a width of 150.
urlConstructor = urlConstructor.height(height: 150)
urlConstructor = urlConstructor.height(height: 400)
let url = urlConstructor.create()
Output:
Let’s now see how different crop mode works. We will try the pad_resize
crop strategy. In this strategy, the output image's dimension (height and width) is the same as requested, no cropping occurs, and the aspect ratio is preserved. This is accomplished by adding padding around the output image to get it to match the exact dimension as requested. You can read more about this here.
urlConstructor = urlConstructor.width(width: 300)
urlConstructor = urlConstructor.height(height: 200)
urlConstructor = urlConstructor.cropMode(cropMode: CropMode.PAD_RESIZE)
let url = urlConstructor.create()
Output:
You can use the ar parameter to change the aspect ratio like this:
urlConstructor = urlConstructor.height(height: 400)
urlConstructor = urlConstructor.aspectRatio(width: 3, height: 2)
let url = urlConstructor.create()
Output:
Chained transformations provide a simple way to control the sequence in which transformations are applied.
Let’s try it out by resizing an image, then rotating it:
urlConstructor = urlConstructor.height(height: 400)
urlConstructor = urlConstructor.width(width: 300)
urlConstructor = urlConstructor.chainTransformation()
urlConstructor = urlConstructor.rotation(rotation: Rotation.VALUE_90)
let url = urlConstructor.create()
Output:
ImageKit.io allows you to add text and image overlay dynamically.
Text overlay can be used to superimpose text on an image. Try it like so:
urlConstructor = urlConstructor.width(width: "400")
urlConstructor = urlConstructor.overlayText(overlayText: "Hand with a green plant")
urlConstructor = urlConstructor.overlayTextColor(overlayTextColor: "264120")
urlConstructor = urlConstructor.overlayTextFontSize(overlayTextSize: 30)
urlConstructor = urlConstructor.overlayX(overlayX: 10)
urlConstructor = urlConstructor.overlayY(overlayY: 20)
let url = urlConstructor.create()
Output:
Image overlay can be used like this:
urlConstructor = urlConstructor.overlayImage(overlayImage: "logo-white_SJwqB4Nfe.png")
urlConstructor = urlConstructor.overlayX(overlayX: 10)
urlConstructor = urlConstructor.overlayY(overlayY: 20)
let url = urlConstructor.create()
Output:
Let's learn how to upload an image to our media library.
iOS SDK provides ImageKitUploader
which provide functions to allow upload files to the ImageKit media library directly from the client-side.
For using upload functionality, we need to pass publicKey
and authenticationEndpoint
while initializing the SDK. Replace your_url_endpoint
, your_public_key
, your_authentication_endpoint
with actual values.
ImageKit.init(
publicKey: "your_public_api_key",
urlEndpoint: "your_url_endpoint",
transformationPosition: TransformationPosition.PATH,
authenticationEndpoint: "your_authentication_endpoint"
)
For this, we would need a dummy backend app to authenticate our upload request. API authentication for upload always happens on the backend for security reasons.
The tutorial repository comes with a sample backend server that we can use.
For this quick start guide, we have provided the sample implementation of the authentication endpoint in using the ImageKit Node.js SDK and Express.
In a new terminal window, navigate to the Server
folder inside the tutorial project and install its npm packages:
cd Server/
npm install
Let's modify index.js
to implement http://localhost:8080/auth
which is our authenticationEndpoint
.
/*
This is our backend server.
Replace your_url_endpoint, your_public_key, your_private_key
with actual values
*/
const express = require('express');
const app = express();
var cors = require('cors');
app.use(cors());
const ImageKit = require('imagekit');
const imagekit = new ImageKit({
privateKey: "your_private_key",
publicKey: "your_public_key",
urlEndpoint: "your_url_endpoint"
})
app.get("/auth", function (req, res) {
var signature = imagekit.getAuthenticationParameters();
res.status(200);
res.send(signature);
});
app.listen(8080, function () {
console.log("Live at Port 8080");
});
Finally, run the backend app.
node index.js
You should see a log line saying that the app is “Live on port 8080”.
If you run curl http://localhost:8080/auth
in the terminal, you should see a JSON response like this. Actual values will vary.
{
token: "5dd0e211-8d67-452e-9acd-954c0bd53a1f",
expire: 1601047259,
signature: "dcb8e72e2b6e98186ec56c62c9e62886f40eaa96"
}
Configure the auth endpoint in the frontend app
Head over to AppDelegate.swift
and ensure the** **authenticationEndpoint
is set to http://localhost:8080/auth
To upload an image to the media library, the ImageKit iOS SDK provides ImageKitUploader
. For complete API reference for the image upload function, check out the docs on the ImageKit iOS Git Repository.
The ImageKit.shared.uploader().upload
function can ingest files through UIImage
, NSData
or Url of a remote image
ImageKit.shared.uploader().upload(
file: image,
fileName: "sample-image.jpg",
useUniqueFilename: true,
tags: ["demo"],
folder: "/",
isPrivateFile: false,
customCoordinates: "",
responseFields: "",
progress: { progress in
// Handle Progress
},
completion: { result in
switch result{
case .success(let uploadAPIResponse):
// Handle Success Response
case .failure(let error as UploadAPIError):
// Handle Upload Error
case .failure(let error):
// Handle Other Errors
}
}
)
After a successful upload, you should see the newly uploaded file in the Media Library of your ImageKit dashboard.
If you don't see the file, check if there are any errors in the error log. Make sure that the private API key has been configured. The server app is running. And the uploaded file type is supported by ImageKit.
The possibilities for image manipulation and optimization with ImageKit are endless. Learn more about it here: