Getting the metadata from an image file on macOS is quite easy using Declares
. Continue reading and I’ll show you how!
Create a new Desktop project in the Xojo IDE and add a new Module
to it (for example, named External
).
Next, add a new method to it using the following data in the associated Inspector Panel:
- Method Name: MetadataForImageFile
- Parameters: source As FolderItem
- Return Type: String
- Scope: Global
Now add the following snippet of code to the Code Editor for the just added method:
Var desc As String #If TargetMacOS // Declares against the "Foundation" framework Declare Function NSClassFromString Lib "Foundation" (clsName As CFStringRef) As Ptr Declare Function FileURLWithPath Lib "Foundation" Selector "fileURLWithPath:" (obj As Ptr, path As CFStringRef) As Ptr Declare Function Description Lib "Foundation" Selector "description" (dict As Ptr) As CFStringRef // Declares against the "ImageIO" framework Declare Function CGImageSourceCreateWithURL Lib "ImageIO" (path As Ptr, options As Ptr) As Ptr Declare Function CGImageSourceCopyPropertiesAtIndex Lib "ImageIO" (imageSource As Ptr, index As Integer, options As Ptr) As Ptr If source <> Nil Then // Getting a Reference to the NSURL class Var nsurl As Ptr = NSClassFromString("NSURL") // Getting a reference to a NSURL object created from the given Path // for the source file Var filePath As Ptr = FileURLWithPath(nsurl, source.NativePath) // Getting a reference to the image object created from the // given NSURL object Var imageRef As Ptr = CGImageSourceCreateWithURL(filePath, Nil) // Getting an NSDictionary containing the Metadata for the image Var dict As Ptr = CGImageSourceCopyPropertiesAtIndex(imageRef, 0, Nil) // Getting the contents of the NSDictionary instance as a String // This will contain the metadata for the image file! desc = Description(dict) End If #EndIf Return desc
Now you can call this method from your code in order to get a String containing the metadata information for the FolderItem passed to it as parameter.
You can find more information from Apple about the ImageIO Framework here. Ask me questions on Twitter @XojoES or on the Xojo Forum.