惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

雷峰网
雷峰网
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
The Cloudflare Blog
博客园_首页
J
Java Code Geeks
A
About on SuperTechFans
人人都是产品经理
人人都是产品经理
量子位
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 三生石上(FineUI控件)
L
LangChain Blog
N
Netflix TechBlog - Medium
Hugging Face - Blog
Hugging Face - Blog
B
Blog
美团技术团队
Microsoft Security Blog
Microsoft Security Blog
P
Proofpoint News Feed
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
宝玉的分享
宝玉的分享
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Last Week in AI
Last Week in AI

ImageKit.io Blog

Next.js Image Optimization with ImageKit Use Video as a Background in Your Next.js Project How to Fix Autoplay Video in Next.js How Durian Scaled a Visual-First Retail Experience to 350K Monthly Visitors Online How Matsmart accelerated image delivery across countries with ImageKit AI in Digital Asset Management: From Smart Workflows to Agentic Automation How Joseph Joseph unified and secured global video delivery with ImageKit How Modall powers fast, effortless media delivery across 40+ projects with ImageKit Digital Asset Management (DAM) Trends: 2026 Report How to add a poster image to Video.js player (and automate it) HLS streaming with Video.js + React Building the future of storytelling with fast, AI-powered video delivery How PushOwl delivers 100M+ image-rich notifications seamlessly with ImageKit How Homify delivers millions of interior design images seamlessly with ImageKit Better event discovery with lightning‑fast videos & images Adding video player in React Native Video player in Angular applications Crop and resize videos in React Next.js image and video upload React image and video upload React video optimization How we quadrupled our traffic to 625K monthly page views How Apollo 24|7 boosted performance & reduced costs with ImageKit Simplify your media workflows with ImageKit DAM integrations Extending Lighthouse for custom image and video optimization analysis Brand Asset Management: What is it? How does it work? WordPress Digital Asset Management Guide - Manage your WP media assets better Why Shopify retailers need a digital asset management solution DAM vs. SharePoint: Which is best for you? AI-powered Metadata and Tagging in Digital Asset Management
Image optimization and file upload in iOS using ImageKit
Abhinav Dhiman · 2020-09-10 · via ImageKit.io Blog

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.