Load and Save Images - macOS Swift Console

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

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 Load and Save 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 Load 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 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/LEADTOOLS22/Resources/Images/image1.cmp" 
let outputFile = "/path/to/LEADTOOLS22/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)") 
    } 
} 

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 creates a new file in the output location specified in the save call.

Screenshot of the console display.

Wrap-up

This tutorial showed how to use the RasterCodecs and RasterImage classes to load and save images.

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.