/ SDKs / iOS
SDKs
Chat SDKs iOS v4
Chat SDKs iOS
Chat SDKs
iOS
Version 4

Generate thumbnails of a file message

Copy link

When sending image or video files, you can create thumbnails of the multimedia files and render them into your UI. You can specify up to three different sizes for thumbnail images when sending a message both with a single file and with multiple files.

Note: Supported file types are image/* and video/*. The Chat SDK doesn't support creating thumbnails when sending a file message via URL.

The sendFileMessage(params:progressHandler:completionHandler:) method requires passing a FileMessageCreateParams object as an argument to a parameter. It contains an array of ThumbnailSize objects which specify the maximum values of width and height of each thumbnail image with make(maxSize:) or make(maxWidth:maxHeight:) constructors. The completionHandler: callback subsequently returns the array of ThumbnailSize objects that each contain the URL of the generated thumbnail image file.

A thumbnail image is generated to fit within the bounds of the provided maxWidth and maxHeight. If the size of the original image is smaller than the specified dimensions, the original image will have the width and height of the specified dimensions. The URL of the thumbnail returns the location of the generated thumbnail file within the Sendbird server.

var thumbnailSizes = [ThumbnailSize]()

// Create and add a ThumbnailSize object.
// 3 thumbnail sizes are allowed.
thumbnailSizes.append(ThumbnailSize.make(maxSize: CGSize(width: 100.0, height: 100.0)))
thumbnailSizes.append(ThumbnailSize.make(maxWidth: 200.0, maxHeight: 200.0))

let params = FileMessageCreateParams(file: FILE)
params.fileName = FILE_NAME
params.fileSize = FILE_SIZE
params.mimeType = MIME_TYPE
params.thumbnailSizes = thumbnailSizes  // Set a ThumbnailSize object to a FileMessageCreateParams object.

channel.sendFileMessage(params: params, progressHandler: nil) { fileMessage, error in
    guard error == nil else {
        // Handle error.
        return
    }

    let first = fileMessage?.thumbnails?[0]
    let second = fileMessage?.thumbnails?[1]

    let maxSizeFirst = first?.maxSize   // 100
    let maxSizeSecond = second?.maxSize // 200

    let urlFirst = first?.url
    let urlSecond = second?.url
}

The same applies to sending a message with multiple files. First, create a MultipleFilesMessageCreateParams object with UploadableFileInfo which sets ThumbnailSizes, an array of three ThumbnailSize instances. They specify the maximum values of width and height of each thumbnail image. Then pass it as an argument in to sendMultipleFilesMessage(). Then the completionHandler: callback subsequently returns the array of ThumbnailSize objects that each contain the URL of the generated thumbnail image file.

let fileInfo = UploadableFileInfo(file: FILE)
fileInfo.fileName = FILE_NAME
fileInfo.fileSize = FILE_SIZE
fileInfo.mimeType = MIME_TYPE
fileInfo.thumbnailSizes = thumbnailSizes  // Set a ThumbnailSize objects to a UploadableFile object.
uploadableFileInfoList.append(fileInfo)
let params = MultipleFilesMessageCreateParams(uploadableFileInfoList: uploadableFileInfoList)
channel.sendMultipleFilesMessage(params: params, fileUploadHandler: nil) { multipleFilesMessage, error in
    guard error == nil else {
        // Handle error.
        return
    }
    let firstFileInfo = multipleFilesMessage?.files.first
    let first = firstFileInfo?.thumbnails?[0]
    let second = firstFileInfo?.thumbnails?[1]
    let maxSizeFirst = first?.maxSize   // 100
    let maxSizeSecond = second?.maxSize // 200
    let urlFirst = first?.url
    let urlSecond = second?.url
}