-
-
Save benbahrenburg/21212e0f5b76d694d5957cc729d56e21 to your computer and use it in GitHub Desktop.
Write XMP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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