iosswiftimagexmp

iOS, Swift, Image Metadata, XMP, DJI Drones


I'm writing an iOS Swift app to fetch metadata from DJI drone images. I'm trying to access the Xmp.drone-dji.X metadata. The iOS/Swift CGImageSource and CGImageMetadata libraries/classes get almost all of the metadata out of the image but not the Xmp.drone-dji. When I get a list of tags, those tag/values are not listed. I know the tags/data are in the images because I've examined the images using exif, exiv2, etc.

Any suggestions?

Here is the code I'm using so far:

result.itemProvider.loadDataRepresentation(forTypeIdentifier: UTType.image.identifier) 
{ data, err in
    if let data = data {

        let src = CGImageSourceCreateWithData(data as CFData,nil)!
        let md = CGImageSourceCopyPropertiesAtIndex(src,0,nil) as! NSDictionary
        let md2 = CGImageSourceCopyMetadataAtIndex(src,0,nil)
    }

Thanks, Bobby


Solution

  • So, after a lot of searching, trial and error, I have found an answer.

    I was not able to get any of the CGImage swift libraries to extract this info for me.

    Adobe has a c++ library that parses xmp/xml data out of images and it purports to support iOS. I didnt want the hassle of building c++ on iOS, importing that into Xcode and then dealing with the fact that thrown errors do not propagate well from c++/objectiveC to Swift.

    So, at a high level, I did the following:

    1. get the bytes of the raw image as CFData or Data then cast to a String

    2. then use String.range() to find beginning of XML/XMP data in image searching for substring <?xpacket begin

    3. use String.range() to find end of XML/XMP data in image using substring <?xpacket end.*?>

    4. Extract the XML document out of image data String

    5. Use Swift XMLParser class to parse the XML and then copying attributes and elements as necessary. I just simply added what I wanted to already existing Exif NSdictionary returned by CGImage classes.

    Happy to answer questions on this approach. My code will eventually be uploaded to GitHub under OpenAthenaIOS project.

    Bobby