This tutorial shows how to load, add, and save annotation objects in a macOS Swift Console application using the LEADTOOLS SDK.
Overview | |
---|---|
Summary | This tutorial covers how to load, add, and save annotations in a macOS Swift Console application. |
Completion Time | 30 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 theAdd References and Set a Licensetutorial, before working on theLoad, Add, and Save Annotations - macOS Swift Consoletutorial.
Start with a copy of the project created in theAdd 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.Annotations.Engine
Leadtools.Codecs.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.
公关所需许可解锁的功能oject. 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 to theSetLicense()
function, create a new instance ofLTAnnCodecs
.
Then, add a new function to your project namedLoadAnnotations(codecs: LTAnnCodecs) -> LTAnnContainer?
. This function takes in theLTAnnCodecs
object created and will return anLTAnnContainer
object containing the loadedLTAnnObjects
. Be sure to call this new function below theLTAnnCodecs
object creation as shown below.
SetLicense()
let annCodecs: LTAnnCodecs = LTAnnCodecs()
guard let annContainer: LTAnnContainer = LoadAnnotations(codecs: annCodecs)else{ fatalError() }
SaveAnnotations(codecs: annCodecs, container: annContainer)
Add the following code to theLoadAnnotations()
function to load the annotation objects from file and return theLTAnnContainer
. For the purposes of this tutorial, this sampleAnnotations XML Filecan be used.
func LoadAnnotations(codecs: LTAnnCodecs) -> LTAnnContainer? {
do{
let xmlString =tryString(contentsOfFile:"FILE PATH TO ANNOTATIONS XML", encoding: .utf8)
// Load annotations
guard let annContainer: LTAnnContainer = codecs.load(from: xmlString, pageNumber: 1)else{ fatalError("Failed to load file to container.") }
// Print all the objects in the container
print("\n\(annContainer.children.count) Annoations Loaded:\n")
foriin0..
let object = annContainer.children[i]
print("Annotation: \(object.friendlyName)")
}
returnannContainer
}catch{
print(error.localizedDescription)
}
returnnil
}
Add a new function to your project namedSaveAnnotations(codecs: LTAnnCodecs, container: LTAnnContainer)
. This function takes in the currentLTAnnCodecs
object and theLTAnnContainer
returned from theLoadAnnotations()
function. Be sure to call theSaveAnnotations()
func below the call to theLoadAnnotations
func, as shown in the previous section.
Add the code below to theSaveAnnotations()
function to add a newLTAnnObject
to theLTAnnContainer
and then export the container to a new XML file.
func SaveAnnotations(codecs: LTAnnCodecs, container: LTAnnContainer) {
// Add new rectangle to container
let annRect: LTAnnRectangleObject = LTAnnRectangleObject()
annRect.rect = LeadRectDMake(100, 200, 400, 400)
annRect.stroke = LTAnnStroke.init(brush: LTAnnSolidColorBrush.init(color:"Red"), withThickness: LeadLengthD.init(value: 3.0))
container.children.add(annRect)
print("\nNew AnnRectangleObject added to annotation container\n")
// Export annotations to XML file
let saveString = codecs.save(container: container, format: LTAnnFormat.annotations, data: nil, savePageNumber: 0)
do{
iflet xml =try? XMLElement.init(xmlString: saveString) {
let str = xml.xmlString(options: .nodePrettyPrint)
trystr.write(to: URL(fileURLWithPath:"FILE PATH TO EXPORT XML FILE"), atomically:true, encoding: .utf8)
}
}catch{
print(error.localizedDescription)
}
print("\(container.children.count) Annotations saved to file")
}
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 loads theLTAnnContainer
from the specified XML file, adds a newLTAnnRectangleObject
to theLTAnnContainer
, and then saves the modified container to a new XML file.
This tutorial showed how to load and save annotation containers, as well as add anLTAnnObject
to theLTAnnContainer
. Also it covered how to use theLTAnnCodecs
class.