ImageKit.io is an image CDN that provides intelligent real-time image optimization, resizing, cropping, and fast CDN delivery. It allows provides a media library with unlimited storage.

ImageKit.io iOS SDK simplifies the URL generation and file uploading in the iOS application.

Prerequisites

If you want to explore how an iOS App can utilize this ImageKit SDK, we have provided a pre-built sample iOS Example app. Refer to this repository for the same.

Basic setup

Here is how you can create a new project and start using the ImageKit iOS SDK.

Step 1: Create iOS Application in XCode

Step 2: Get the ImageKit iOS SDK code using Cocoapods

If you don’t have Cocoapods set up in your project, run pod init.

Add ImageKitIO to Podfile.

target 'MyApp' do
  pod 'ImageKitIO', '~> 1.0.0'
end

Then, run the command.

pod install

Step 3: Get your ImageKit Public Key

In your ImageKit account dashboard, go to the developer section to find your API Keys section from the navigation bar.  Note down your public API key.

Step 4: Initialize ImageKit object in AppDelegate.swift file


import ImageKitIO
...
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { 
  ...
  _ = ImageKit.init(
          clientPublicKey: "your_public_key", 
          imageKitEndpoint: "https://ik.imagekit.io/your_imagekit_id", 
          transformationPosition: TransformationPosition.PATH, 
          authenticationEndpoint: "http://localhost:8080/auth"
  ) 
  ....
  return true 
}
...

publicKey and urlEndpoint are mandatory parameters for SDK initialization.

publicKey should be your ImageKit account's public key, which we discovered in step 3.

authenticationEndpoint is required if you want to use the SDK for client-side file uploads.  The SDK makes an HTTP GET request to this endpoint and expects a JSON response with three fields i.e. signature, token and expire. You must implement this API endpoint in your backend using ImageKit.io server-side SDKs. Learn more about this process.

Once you've configured these and the backend API to generate a signature for uploads, you can start using the SDK to fetch transformed images from or upload new files to your ImageKit account.

URL generation

There are multiple ways in which you can generate a URL for an image.

Specifying a transformation and generating an image URL

Using default urlEndpoint defined while initalizing the SDK:

// https://ik.imagekit.io/your_imagekit_id/tr:h-400,ar-3-2/default-image.jpg
let url = ImageKit.shared.url(
            path: "default-image.jpg"
        )
	.height(height: 400)
	.aspectRatio(width: 3, height: 2)
	.create()

Using custom urlEndpoint

In case you want to override the default URL endpoint provided during initilization.

// https://images.your_domain.com/tr:h-400,ar-3-2/default-image.jpg
let url = ImageKit.shared.url(
			urlEndpoint: "https://images.your_domain.com",
            path: "default-image.jpg"
        )
	.height(height: 400)
	.aspectRatio(width: 3, height: 2)
	.create()

Using src parameter

In case you already have an absolute image URL. You can use src parameter.

// https://ik.imagekit.io/your_imagekit_id/default-image.jpg?tr=h-400,ar-3-2
let url = ImageKit.shared.url(
			src: "https://ik.imagekit.io/your_imagekit_id/default-image.jpg"
        )
	.height(height: 400)
	.aspectRatio(width: 3, height: 2)
	.create()

Transformations can be chained onto the ImagekitUrlConstructor, as following:

// https://ik.imagekit.io/your_imagekit_id/default-image.jpg?tr=h-400,ar-3-2
let imagekitUrlConstructor: ImagekitUrlConstructor = ImageKit.shared.url(
            path: "default-image.jpg",
            transformationPosition: TransformationPosition.QUERY
        )
imagekitUrlConstructor =  imagekitUrlConstructor.height(height: 400)
imagekitUrlConstructor =  imagekitUrlConstructor.aspectRatio(width: 3, height: 2)
let url = imagekitUrlConstructor.create()

You can refer to ImageKitURLConstructor.swift file in sample application provided with SDK for the list of all transforms.

You can pass any custom parameter using .addCustomTransformation("custom_param", "custom_value") function.

File uploading

The SDK enables you to upload a file directly to ImageKit Media Library.

Note: If you want to use the ImageKit SDK to upload files to Media Library, authenticationEndpoint is required.

You can pass any option supported by the upload API. Authentication related parameters are added by the SDK itself like publicKey, signature, expire, and token.

Upload file from UIImage

let image: UIImage
ImageKit.shared.uploader().upload(
  file: image,
  fileName: "sample-image.jpg",
  useUniqueFilename: true,
  tags: ["demo"],
  folder: "/path/of/destination/folder/",
  isPrivateFile: false,
  customCoordinates: "10,10,100,100",
  responseFields: "tags,isPrivateFile",
  progress: { progress in
  	// Handle Progress
  },
  completion: { result in 
  	// Handle upload response as returned from the upload API
  }
)

Upload file from Data

let data : Data
ImageKit.shared.uploader().upload(
  file: data,
  fileName: "sample-image.jpg",
  useUniqueFilename: true,
  tags: ["demo"],
  folder: "/",
  isPrivateFile: false,
  customCoordinates: "",
  responseFields: "",
  progress: { progress in
  	// Handle Progress
  },
  completion: { result in 
  	// Handle Upload Response
  }
)

Upload file from Remote URL

ImageKit.shared.uploader().upload(
  file: "https://www.example.com/sample-image.jpg",
  fileName: "sample-image.jpg",
  useUniqueFilename: true,
  tags: ["demo"],
  folder: "/",
  isPrivateFile: false,
  customCoordinates: "",
  responseFields: "",
  progress: { progress in
  	// Handle Progress
  },
  completion: { result in 
  	// Handle Upload Response
  }
)

Conclusion

The ImageKit iOS SDK solves the challenges that iOS developers face related to file upload and quick loading. Transformations enable you to deliver high-quality images in different sizes for different devices as per the layout.

If you have any questions, create an issue on the Github repo or contact us at support@imagekit.io.