This tutorial shows how to load and save images with the RasterCodecs
and RasterImage
classes in a macOS Swift Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to use the RasterCodecs class to load and save images in a macOS Swift Console application. |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (4 KB) |
Platform | macOS Swift Console Application |
IDE | Xcode |
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 Load and Save Images - 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 <INSTALL_DIR>\LEADTOOLS23\Bin\Xcode\Frameworks\macOS
:
Leadtools.framework
Leadtools.Codecs.framework
Leadtools.Codecs.Cmp.framework
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.
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.
With the project created, the references added, and the license set, coding can begin.
Using the Project Navigator, open main.swift
. below the SetLicense()
call, add a new instance of RasterCodecs
, along with two string values containing the file paths to the input and output files.
SetLicense()
let codecs = LTRasterCodecs()
let inputFile = "/path/to/LEADTOOLS23/Resources/Images/image1.cmp"
let outputFile = "/path/to/LEADTOOLS23/Resources/Images/test.jpg"
if let image = LoadImage(inputFile: inputFile, codecs: codecs) {
SaveImage(image: image, outputFile: outputFile, codecs: codecs)
}
Add two new functions named LoadImage(inputFile: String, codecs: LTRasterCodecs)
and SaveImage(image: LTRasterImage, outputFile: String, codecs: LTRasterCodecs)
. Call these functions beneath the SetLicense()
call, as shown above.
Add the code below to the LoadImage()
function to load the image in the given file path as a RasterImage
object.
func LoadImage(inputFile: String, codecs: LTRasterCodecs) -> LTRasterImage? {
do {
let image: LTRasterImage = try codecs.load(file: inputFile)
print("Image loaded successfully")
return image
} catch {
print("Failed to load image.\n\(error.localizedDescription)")
return nil
}
}
Add the following code to the SaveImage()
function to convert the image to a JPEG image and export it to the given file path.
func SaveImage(image: LTRasterImage, outputFile: String, codecs: LTRasterCodecs) {
do {
try codecs.save(image, file: outputFile, format: LTRasterImageFormat.jpeg, bitsPerPixel: 0)
print("Image saved successfully")
} catch {
print("Failed to save image.\n\(error.localizedDescription)")
}
}
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 creates a new file in the output location specified in the save call.
This tutorial showed how to use the RasterCodecs
and RasterImage
classes to load and save images.