Create a Multipage File from Multiple Images - macOS Swift Console

This tutorial shows how to merge images into one multipage TIFF file with the RasterCodecs class in a macOS Swift Console application using the LEADTOOLS SDK.

Overview  
Summary This tutorial covers how to merge images into a multipage file in a macOS Swift Console application.
Completion Time 30 minutes
Project Download tutorial project (5 KB)
Platform Swift macOS Console Application
IDE Xcode
Runtime License Download LEADTOOLS
Try it in another language

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Create a Multipage File from Multiple Images - macOS Swift Console tutorial.

Create the Project and Add LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. This tutorial requires the following Framework references located at <INSTALL_DIR>\LEADTOOLS22\Bin\Xcode\Frameworks\macOS:

Edit the Leadtools-Bridging-Header.h file to add the following imports:

#import <Leadtools.Codecs/Leadtools.Codecs.h> 

For a complete list of which files are required for your application, refer to Files to be Included in your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.

Add the Merge Image Code

With the project created, the references added, and the license set, coding can begin.

Using the Project Navigator, open main.swift. Below the call to SetLicense(), add an array of string values that will contain the files to merge. Below the new array, add the code below.

SetLicense() 
 
var files:[String] = [] 
 
let imageDir: String = "/path/to/LEADTOOLS22/Resources/Images" 
for file in try FileManager.default.contentsOfDirectory(atPath: imageDir) { 
    let fileURL = URL(fileURLWithPath: file) 
    if fileURL.pathExtension == "cmp" { 
        files.append(imageDir + "/" + file) 
    } 
} 
 
let multipageFile = "/path/to/LEADTOOLS22/Resources/Images/merged.tif" 
MergeFiles(files: files, outputFile: multipageFile) 

For the purposes of this tutorial, the array will contain all the CMP files in the <INSTALL_DIR>/LEADTOOLS22/Resources/Images directory.

Add a new function to main.swift named MergeFiles(files: [String], outputFile: String). call this function as shown above. Add the code below to the new function to load each of the CMP files appending each file to make a multipage TIFF file.

func MergeFiles(files: [String], outputFile: String) { 
    let codecs: LTRasterCodecs = LTRasterCodecs() 
    for file in files { 
        print("Adding file: \(file)") 
        do { 
            let image: LTRasterImage = try codecs.load(file: file) 
            try codecs.save(image, file: outputFile, format: LTRasterImageFormat.tifJpeg411, bitsPerPixel: 0, firstPage: 1, lastPage: -1, firstSavePageNumber: 1, pageMode: LTCodecsSavePageMode.append) 
        } catch { 
            print(error.localizedDescription) 
        } 
    } 
} 

Run the Project

Clean the project to clear any errors by selecting Product -> Clean Build Folder or Shift + Command + K.

Run the project by selecting Product -> Run or Command + R.

If the steps were followed correctly, the application runs and either creates a new file named merged.tif, or appends pages to that file if it already exists. This file should contain all the CMP images from the LEADTOOLS Images directory.

Screenshot of the console display.

Wrap-up

This tutorial showed how to save TIFF images as well as how to merge images into a multipage file.

See Also

Help Version 22.0.2024.3.20
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.