This tutorial shows how to read and write TIFF tags and comments in a macOS Swift Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to work with TIFF tags and comments in a macOS Swift Console application. |
Completion Time | 20 minutes |
Visual Studio Project | Download tutorial project (5 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 Read and Write TIFF Tags and Comments - macOS Swift Console tutorial.
Start with a copy of the project created in the Load and Save Images 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.Codecs.Cmp.framework
Leadtools.Codecs.Fax.framework
Leadtools.Codecs.Tif.framework
Leadtools.Codecs.framework
Leadtools.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
. Add a new string fileName
below the SetLicense();
method call, and set it to the file path containing your TIFF file.
Add a new function named ReadAndWriteTifComments(file: String)
and call it after the created string value, as shown below.
SetLicense()
let fileName: String = "/path/to/LEADTOOLS23/Resources/Images/clean.tif"
ReadAndWriteTifComments(file: fileName)
ReadAndWriteTifTags(file: fileName)
Add the code below to the ReadAndWriteTifComments()
function to write a comment to the file, then read that same comment inside the file and display it to the console.
func ReadAndWriteTifComments(file: String) {
let codecs: LTRasterCodecs = LTRasterCodecs()
// Write the comment to the file
let writeComment: LTRasterCommentMetadata = LTRasterCommentMetadata()
writeComment.type = .software
writeComment.fromAscii("LEADTOOLS Demo")
do {
try codecs.write(comment: writeComment, to: fileName, pageNumber: 1)
} catch {
print(error.localizedDescription)
}
// Read the comment
do {
let readComment: LTRasterCommentMetadata = try codecs.readComment(from: fileName, pageNumber: 1, type: .software)
print("The following comment has been read:\n\(readComment.toAscii())/n")
} catch {
print(error.localizedDescription)
}
}
Add a new function called ReadAndWriteTifTags(file: String)
. Call this function after the ReadAndWriteTifComments(file: fileName)
line of code. Add the code below to read the XResolution
of the image, modify it, and write the new XResolution
tag.
func ReadAndWriteTifTags(file: String) {
let codecs: LTRasterCodecs = LTRasterCodecs()
// This code reads the Xresolution from a TIFF image, modifies the value, and saves it back.
let xresTagID: Int = 282
var readTag: LTRasterTagMetadata?
do {
let tags: [LTRasterTagMetadata] = try codecs.readTags(from: file, pageNumber: 1)
for tag in tags {
if tag.tagId == xresTagID {
readTag = tag as LTRasterTagMetadata
break
}
}
var image: LTRasterImage = try codecs.load(file: file)
print("X Resolution before: \(image.xResolution)")
var rational = UnsafeMutablePointer<LTRasterMetadataURational>.allocate(capacity: readTag!.data.count).pointee
readTag!.toURational(&rational, itemCount: 1)
rational.numerator = rational.numerator * 5
rational.denominator = rational.denominator * 1
readTag!.fromURational(&rational, itemCount: 1)
try codecs.write(tag: readTag, to: file, pageNumber: 1)
print("Resolution changed successfully.")
image = try codecs.load(file: file)
print("X Resolution after: \(image.xResolution)")
} catch {
print(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 writes a new TIFF comment and then displays the comment in the console. The application also reads the XResolution
from the TIFF image, modifies the resolution value, and writes the tag back to the TIFF image.
This tutorial showed how to use the LTRasterTagMetadata
and LTRasterCommentMetadata
classes to read/write TIFF comments and tags.