Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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=\"\"><rdf:Description rdf:about=\"\" xmlns:apple_desktop=\"\"><apple_desktop:solar>\(plist)</apple_desktop:solar></rdf:Description></rdf:RDF></x:xmpmeta>"
let newData = .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)
let writer = Writer()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment