/optidash-swift

Primary LanguageSwiftMIT LicenseMIT

Optidash

Optidash is a modern, AI-powered image optimization and processing API.
We will drastically speed-up your websites and save you money on bandwidth and storage.


The official Swift integration for Optidash API.


Documentation

See the Optidash API docs.

Installation with Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application. You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Optidash into your Xcode project using Carthage, specify it in your Cartfile:

github "optidash-ai/optidash-swift"

Installation with CocoaPods

source 'https://github.com/CocoaPods/Specs.git'
pod 'Optidash', '~> 1.0.0'

Quick examples

Optidash API enables you to provide your images for processing in two ways - by uploading them directly to the API (Image Upload) or by providing a publicly available image URL (Image Fetch).

You may also choose your preferred response method on a per-request basis. By default, the Optidash API will return a JSON response with rich metadata pertaining to input and output images. Alternatively, you can use binary responses. When enabled, the API will respond with a full binary representation of the resulting (output) image. This Swift module exposes two convenience methods for interacting with binary responses: .toFile() and .toBuffer().

Image upload

Here is a quick example of uploading a local file for processing. It calls .toJson() at a final step and instructs the API to return a JSON response.

// Pass your Optidash API Key to the constructor
let opti = OptidashClient(key: "YOUR-API-KEY")

// Upload an image from disk and resize it to 1024 x 768,
// using 'fit' mode and gravity set to 'bottom'
let fileUrl = Bundle.main.url(forResource: "sample", withExtension: "jpg")!
let parameters: [String : Any] = [
    "width": 1024,
    "height": "768",
    "mode": "fit",
    "gravity": "bottom"
]

do {
    try opti.upload(fileUrl: fileUrl)
            .resize(parameters)
            .toJson() { [weak self] (result) in
                switch (result) {
                case .success(let data):
                    DispatchQueue.main.async {
                        let response = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
                        print(response!)
                    }
                case .failure(let error):
                    DispatchQueue.main.async {
                        print(error)
                    }
                }
            }
} catch let error {
    print(error)
}

Image fetch

If you already have your source visuals publicly available online, we recommend using Image Fetch by default. That way you only have to send a JSON payload containing image URL and processing steps. This method is also much faster than uploading a full binary representation of the image.

// Pass your Optidash API Key to the constructor
let opti = OptidashClient(key: "YOUR-API-KEY")

// Provide a publicly available image URL with `.fetch()` method
// and resize it to 1024 x 768 using 'fit' mode and gravity set to 'bottom'
let imageURL = URL(string: "https://www.website.com/image.jpg")!
let parameters: [String : Any] = [
    "width": 1024,
    "height": "768",
    "mode": "fit",
    "gravity": "bottom"
]

do {
    try opti.fetch(url: imageURL)
            .resize(parameters)
            .toJson() { [weak self] (result) in
                switch (result) {
                case .success(let data):
                    DispatchQueue.main.async {
                        let response = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
                        print(response!)
                    }
                case .failure(let error):
                    DispatchQueue.main.async {
                        print(error)
                    }
                }
            }
} catch let error {
    print(error)
}

License

This software is distributed under the MIT License. See the LICENSE file for more information.