Load, Add, and Save Annotations - macOS Swift Console

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
  • Apple:macOS Console
  • Java:Java

Required Knowledge

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.

Create the Project and Add LEADTOOLS References

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\LEADTOOLS22\Bin\Xcode\Frameworks\macOS:

Edit theLeadtools-Bridging-Header.hfile 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.

Set the License File

公关所需许可解锁的功能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.

Add the Load Annotations Code

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 theLTAnnCodecsobject created and will return anLTAnnContainerobject containing the loadedLTAnnObjects. Be sure to call this new function below theLTAnnCodecsobject 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 annotationsguard let annContainer: LTAnnContainer = codecs.load(from: xmlString, pageNumber: 1)else{ fatalError("Failed to load file to container.") }// Print all the objects in the containerprint("\n\(annContainer.children.count) Annoations Loaded:\n")foriin0..let object = annContainer.children[i]print("Annotation: \(object.friendlyName)")}returnannContainer}catch{print(error.localizedDescription)}returnnil}

Add and Save Annotations Code

Add a new function to your project namedSaveAnnotations(codecs: LTAnnCodecs, container: LTAnnContainer). This function takes in the currentLTAnnCodecsobject and theLTAnnContainerreturned from theLoadAnnotations()function. Be sure to call theSaveAnnotations()func below the call to theLoadAnnotationsfunc, as shown in the previous section.

Add the code below to theSaveAnnotations()function to add a newLTAnnObjectto theLTAnnContainerand then export the container to a new XML file.

func SaveAnnotations(codecs: LTAnnCodecs, container: LTAnnContainer) {// Add new rectangle to containerlet 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 filelet 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")}

Run the Project

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 theLTAnnContainerfrom the specified XML file, adds a newLTAnnRectangleObjectto theLTAnnContainer, and then saves the modified container to a new XML file.

Screenshot of the console display.

Wrap-up

This tutorial showed how to load and save annotation containers, as well as add anLTAnnObjectto theLTAnnContainer. Also it covered how to use theLTAnnCodecsclass.

See Also

iOS
188金宝搏的网址客服|Support|Contact Us|Intellectual Property Notices
© 1991-2023LEAD Technologies, Inc.All Rights Reserved.