This tutorial shows how to merge images into one multipage TIFF file with theRasterCodecs
class 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 |
|
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.
从项目中创建的副本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
:
Leadtools.framework
Leadtools.Codecs.framework
Leadtools.Codecs.Cmp.framework
Edit theLeadtools-Bridging-Header.h
file 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.
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.
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
directory.
Add a new function tomain.swift
namedMergeFiles(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)
}
}
}
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.
This tutorial showed how to save TIFF images as well as how to merge images into a multipage file.