ImageExporter

@objcMembers
public class ImageExporter

The ImageExporter is responsible for exporting SessionImage objects to UIImage objects and JPEG files.

For example, you can export to a UIImage with the following:

if let image = session.image {
    ImageExporter.shared.export(image: image, completion: { (error, uiImage) in
        if let error = error {
            print("Unable to export image: \(error)")
            return
        }

        print("Finished image export with UIImage: \(uiImage!)")
    })
}

In addition to exporting as a UIImage, the exported image is saved to file as a JPEG at the exportedImageURL variable. After the export has completed, you may use, move or copy this file. Once you are finished with the file, you should call destroy() on the session in order to remove the image from the users drafts and delete its associated files.

If you do not provide an API key or it is invalid, exported images will include a watermark.

Getting the Shared ImageExporter

  • Use this to access the shared instance of the ImageExporter. For example to access the export status:

    print("Is exporting: \(ImageExporter.shared.isExporting)")
    

    Declaration

    Swift

    public static let shared: ImageExporter

Export Status

  • Indicates if the ImageExporter is currently exporting anything.

    Default value: false

    Declaration

    Swift

    public var isExporting: Bool { get }

Image Exports

  • Exports a SessionImage object. After the export is done, the completion closure will be called with either an error or the exported UIImage. A JPEG file will also be available at the exportedImageURL variable. You can move, copy or delete this file at your own discretion.

    Attention

    When this is called, any other existing image export will be cancelled.

    Declaration

    Swift

    public func export(image: SessionImage, compressionQuality: Float = 0.95, completion: @escaping (ImageExporterError?, UIImage?) -> Void)

    Parameters

    image

    The image to export.

    compressionQuality

    The compression quality for the JPEG image saved to file at the exportedImageURL variable. This has no effect on the UIImage passed to the completion closure. The value of this must be in a 0.0 to 1.0 range. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).

    completion

    This closure will be called when the export succeeds or fails. If the export failed there will be an error present, otherwise the exported UIImage will be present. No compression is applied to the UIImage but only to the saved JPEG file.

  • Exports multiple SessionImage objects. After the export is done, the completion closure will be called with either an error or the exported UIImages. JPEG files will also be available at the exportedImageURL variable for each image. You can move, copy or delete these files at your own discretion.

    Attention

    When this is called, any other existing image export will be cancelled.

    Declaration

    Swift

    public func export(images: [SessionImage],
                        compressionQuality: Float = 0.95,
                        progress: ((_ progress: Double) -> Void)?  = nil,
                        completion: @escaping (Error?, [UIImage]?) -> Void)

    Parameters

    image

    The image to export.

    compressionQuality

    The compression quality for the JPEG image saved to file at the exportedImageURL variable. This has no effect on the UIImages passed to the completion closure. The value of this must be in a 0.0 to 1.0 range. The value 0.0 represents the maximum compression (or lowest quality) while the value 1.0 represents the least compression (or best quality).

    progress

    This closure will be called each time the export progress is updated. The parameter will contain the progress of the export in a 0.0 to 1.0 range.

    completion

    This closure will be called when the export succeeds or fails. If the export failed there will be an error present, otherwise the exported UIImages will be present. No compression is applied to the UIImages but only to the saved JPEG file.

Cancel Export

  • Cancels any existing image export.

    Declaration

    Swift

    public func cancelExport()