All Products
Search
Document Center

Intelligent Media Management:IMM SDK for Swift

Last Updated:Mar 20, 2025

Alibaba Cloud OpenAPI Explorer provides API documentation, API debugging, and SDK examples to help you get started with API development. This topic describes how to install and use Intelligent Media Management (IMM) SDK for Swift.

Prerequisites

  • Swift 3.0 or later is installed.

  • An AccessKey pair is created and obtained. For more information, see Create an AccessKey pair.

  • OSS is activated, a bucket is created, and objects are uploaded to the bucket. For more information, see Upload objects.

  • IMM is activated. For more information, see Activate IMM.

  • A project is created in the IMM console. For more information, see Create a project.

    Note
    • You can call the CreateProject operation to create a project. For more information, see CreateProject.

    • You can call the ListProjects operation to query the existing projects in a specific region. For more information, see ListProjects.

Install the SDK

Ad the dependency to the Package.swift file.

import PackageDescription

let package = Package(
    name: "immdemo",
    dependencies: [
        .package(url: "https://github.com/alibabacloud-sdk-swift/imm-20200930/", from: "2.1.0")
    ]
)

Sample code

The following sample code provides an example on how to use IMM SDK for Swift to detect face information in an image stored in the China (Hangzhou) region.

Note

When you use the following sample code, replace the domain name with a domain name in the region where your IMM project resides. Make sure that the IMM project and OSS object reside in the same region.

#!/usr/bin/env xcrun swift

import Cocoa
import Foundation
import Tea
import AlibabacloudImm20200930
import AlibabacloudOpenApi
import TeaUtils

open class Client {
    public static func createClient(_ accessKeyId: String?, _ accessKeySecret: String?) throws -> AlibabacloudImm20200930.Client {
        var config: AlibabacloudOpenApi.Config = AlibabacloudOpenApi.Config([
            "accessKeyId": accessKeyId as! String,
            "accessKeySecret": accessKeySecret as! String
        ])
        // Specify the endpoint for the region which the IMM project resides. 
        config.endpoint = "imm.cn-hangzhou.aliyuncs.com"
        return AlibabacloudImm20200930.Client(config)
    }

    @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
    public static func main(_ args: [String]?) async throws -> Void {
        // Specify the AccessKey ID and AccessKey secret of the RAM user. 
        var client: AlibabacloudImm20200930.Client = try Client.createClient("accessKeyId", "accessKeySecret")
        var detectImageFacesRequest: AlibabacloudImm20200930.DetectImageFacesRequest = AlibabacloudImm20200930.DetectImageFacesRequest([
            // Specify the name of the IMM project. 
            "projectName": "immtest",
            // Specify the URI of the image in OSS. 
            "sourceURI": "oss://test-bucket/test-object.jpg"
        ])
        var runtime: TeaUtils.RuntimeOptions = TeaUtils.RuntimeOptions([:])
        do {
            // Initiate the request. 
            try await client.detectImageFacesWithOptions(detectImageFacesRequest as! AlibabacloudImm20200930.DetectImageFacesRequest, runtime as! TeaUtils.RuntimeOptions)
        }
        catch {
            if error is Tea.TeaError {
                try TeaUtils.Client.assertAsString(error.message)
            } else {
                throw error
            }
        }
    }
}

Client.main(CommandLine.arguments)