Create a Multipage File from Multiple Images - macOS Swift Console

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

Overview
苏mmary This tutorial covers how to merge images into a multipage file in a macOS Swift Console application.
Completion Time 30分钟
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 theAdd References and Set a Licensetutorial, before working on theCreate a Multipage File from Multiple Images - macOS Swift Consoletutorial.

Create the Project and Add LEADTOOLS References

从项目中创建的副本Add References and Set a Licensetutorial. 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\LEADTOOLS22\Bin\Xcode\Frameworks\macOS:

Edit theLeadtools-Bridging-Header.hfile to add the following imports:

#import 

For a complete list of which files are required for your application, refer toFiles 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 toSetting a Runtime License.

There are two types of runtime licenses:

Note

Adding LEADTOOLS references and setting a license are covered in more detail in theAdd References and Set a Licensetutorial.

Add the Merge Image Code

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

Using theProject Navigator, openmain.swift. Below the call toSetLicense(), 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"forfileintryFileManager.default.contentsOfDirectory(atPath: imageDir) {let fileURL = URL(fileURLWithPath: file)iffileURL.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/LEADTOOLS22/Resources/Imagesdirectory.

Add a new function tomain.swiftnamedMergeFiles(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()forfileinfiles {print("Adding file: \(file)")do{let image: LTRasterImage =trycodecs.load(file: file)trycodecs.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 selectingProduct -> Clean Build FolderorShift + Command + K.

Run the project by selectingProduct -> RunorCommand + R.

If the steps were followed correctly, the application runs and either creates a new file namedmerged.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

iOS
188金宝搏的网址客服|苏pport|Contact Us|Intellectual Property Notices
© 1991-2022LEAD Technologies, Inc.All Rights Reserved.