This tutorial shows how to read a given image file and detect the proper format, along with the appropriate file extension in a macOS Swift Console application using the LEADTOOLS SDK.
This is useful if the file does not have a file extension or if you are reading the file from a stream.
|Summary||This tutorial covers how to use the
|Completion Time||15 minutes|
|Xcode Project||Download tutorial project (5 KB)|
|Platform||macOS Swift Console Application|
|Development License||Download LEADTOOLS|
|Try it in another language|
Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on the Detect Image Format and Extension - macOS Swift Console tutorial.
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
You will need the individual file codecs for each file type that you are expecting to work with. The toolkit loads the codecs in a certain order when detecting a file format. The following are loaded first:
The individual file codecs do not have to be added to the project directly. They can simply reside in the build folder and will be called at runtime. For more information about loading particular file codecs, including preloading and ignoring codecs, refer to Loading File Codecs.
Leadtools-Bridging-Header.h file to add the following imports:
For a complete list of which files are required for your application, refer to Files 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 to Setting a Runtime License.
There are two types of runtime licenses:
Adding LEADTOOLS references and setting a license are covered in more detail in the Add References and Set a License tutorial.
With the project created, the references added, and the license set, coding can begin.
Using the Project Navigator, open
main.swift. Add the following code below the call to the
SetLicense function, which will gather the information on the given file and output the file extension.
let inputFile = "<INSTALL_DIR>/LEADTOOLS22/Resources/Images/ocr1.tif"
// Retrieve information on the file without fully loading it
let codecs: LTRasterCodecs = LTRasterCodecs()
let info: LTCodecsImageInfo = try codecs.imageInformation(file: inputFile, totalPages: false)
// Get the proper RasterImageFormat and the friendly name for it
let format: LTRasterImageFormat = info.format
let ext: String = LTRasterCodecs.extension(for: format)
print("This file has the extension \(ext)")
For the purposes of this tutorial the TIFF image in the following file path is used:
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 gathers the image file format and extension and then displays it to the console.
This tutorial showed how to gather the image file format and extension using the
CodecsImageInfo class and
GetExtension() method. We also covered how to use the