How to upload images to Cloudinary

Building a social media site where users can share images can get cumbersome on the backend. Luckily, cloud technology like Amazon S3 exist to do all the heavy lifting.

This tutorial uses Cloudinary. Similar to S3, Cloudinary specifically works with images. This tutorial covers setting up an account and uploading images from a native iOS application.

TL;DR: Download the source code here.

Setup

  1. Register a free Cloudinary account.

  2. Visit the Cloudinary Console. Note the Cloud Name, API Key, and API Secret.

Image

  1. From the Upload Settings page. Scroll to Upload Presets and click Add upload preset

Image

  1. From Add upload preset, enter these settings:

    Preset name: photo-upload,
    Mode: unsigned.
    Type: upload
    Access Mode: public.
    Upload format: jpg

Access Mode set to public allows anyone with a URL to view the uploaded photo. If you need tigher security, set this to authenticated mode.

png format is also an available. Use this in cases where images can be transparent.

Setup your project

Setup the Xcode project by creating a new single view app.

Cloudinary’s iOS API is easiest to install using Cocoapods. Edit your project’s Podfile and install.

1
2
3
use_frameworks!

pod 'Cloudinary'

Make sure to close the Xcode project and open the newly created .xcworkspace file by Cocoapods.

Code

Before you can upload anything your app will need to be configured. Using the values you noted from Setup step 2 fill in this section of CloudinaryClient.swift.

1
2
3
private var apiKey = "< YOUR API KEY HERE >";
private var apiSecret = "< YOUR API SECRET HERE >";
private var cloudName = "< YOUR CLOUD NAME HERE >";

To upload an image using CloudinaryClient call the static uploadPhoto function from anywhere in your code.

1
2
3
CloudinaryClient.uploadPhoto(image) { url, error in
print(">>>>>>> \(url)")
}

And voila, an https url for your image is given in the callback function. No need for database blobs, complex PUT requests, or size limits.

last update time 2018-09-04