Skip to content

Instantly share code, notes, and snippets.

@steve228uk
Created June 7, 2018 12:58
Show Gist options
  • Save steve228uk/1aa64b91e6308d5a92d42a883f0654ef to your computer and use it in GitHub Desktop.
Save steve228uk/1aa64b91e6308d5a92d42a883f0654ef to your computer and use it in GitHub Desktop.
Write XMP
import UIKit
import CoreServices
import ImageIO
class Writer {
let output = NSTemporaryDirectory().appending("output.heic")
lazy var outputUrl: CFURL = {
return URL(fileURLWithPath: output) as CFURL
}()
/// The plist to encode
let plist = "123"
func run() {
if let file = Bundle.main.url(forResource: "test", withExtension: "heic"),
let data = try? Data(contentsOf: file) as CFData,
let source = CGImageSourceCreateWithData(data, nil) {
// Get the UTI for the image (maybe we should harcode this as public.heic)
let uti = CGImageSourceGetType(source)!
// Get the count of image source
let count = CGImageSourceGetCount(source)
// Create the destination
let destination = CGImageDestinationCreateWithURL(outputUrl, uti, count, nil)!
// Create the new XMP metadata
let toWrite = "<x:xmpmeta xmlns:x=\"adobe:ns:meta/\" x:xmptk=\"XMP Core 5.4.0\"><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\"><rdf:Description rdf:about=\"\" xmlns:apple_desktop=\"http://ns.apple.com/namespace/1.0/\"><apple_desktop:solar>\(plist)</apple_desktop:solar></rdf:Description></rdf:RDF></x:xmpmeta>"
let newData = toWrite.data(using: .ascii)! as CFData
let meta = CGImageMetadataCreateFromXMPData(newData)!
// Set the options to include metadata
let destOptions: [String: AnyObject] = [
kCGImageDestinationMergeMetadata as String: NSNumber(value: false),
kCGImageDestinationMetadata as String: meta
]
var error: Unmanaged<CFError>?
withUnsafeMutablePointer(to: &error) { (ptr) in
CGImageDestinationCopyImageSource(destination, source, destOptions as CFDictionary, ptr)
}
print("ERROR: ", error)
CGImageDestinationFinalize(destination)
}
}
}
let writer = Writer()
writer.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment