iosimagemetadatansdataxmp

Read XMP metadata from NSData


Situation: For an upload; I need a client side check if an image is a 360° image from a camera like the Theta S or Gear 360. This should be fairly easily done by checking the XMP metadata.

However, ImageIO seems to ignore the XMP metadata, if you run the example below, there is no XMP-entry in the data ImageIO returns.

Swift 3:

import ImageIO

let url = URL(fileURLWithPath: filePath)
let imageData:Data = try! Data(contentsOf: url)

if let imageSource = CGImageSourceCreateWithData(imageData as CFData, nil),
   let imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, nil) as? [AnyHashable:Any]
{
    print(imageProperties) // no xmp metadata?
}

Is there any way I can substract the XMP-xml from the images NSData?


Solution

  • If just checking for one specific value in the XMP metadata, instead of enumerating, you can also search for it like this:

    guard let source = CGImageSourceCreateWithData(self as NSData, nil) else { return }
    guard let metadata = CGImageSourceCopyMetadataAtIndex(source, 0, nil) else { return }
    let tag = CGImageMetadataCopyTagWithPath(metadata, nil, "GPano:ProjectionType" as NSString)