Perry Wang
Perry Wang

Reputation: 191

CGImageMetadataSetTagWithPath error in certain tags

I extracted the gain map info from an image using

let url = Bundle.main.url(forResource: "IMG_1181", withExtension: "HEIC")
let source = CGImageSourceCreateWithURL(url! as CFURL, nil)
let portraitData = CGImageSourceCopyAuxiliaryDataInfoAtIndex(source!, 0, kCGImageAuxiliaryDataTypeHDRGainMap) as! [AnyHashable : Any]
let metaData = portraitData[kCGImageAuxiliaryDataInfoMetadata] as! CGImageMetadata

Then I printed all the metadata tags

func printMetadataProperties(from metadata: CGImageMetadata) {
    guard let tags = CGImageMetadataCopyTags(metadata) as? [CGImageMetadataTag] else {

        return
    }

    for tag in tags {
        if let prefix = CGImageMetadataTagCopyPrefix(tag) as String?,
            let namespace = CGImageMetadataTagCopyNamespace(tag) as String?,
           let key = CGImageMetadataTagCopyName(tag) as String?,
           let value = CGImageMetadataTagCopyValue(tag){
            print("Namespace: \(namespace), Key: \(key), Prefix: \(prefix), value: \(value)")
        } else {
            
        }
    }
}
//Namespace: http://ns.apple.com/ImageIO/1.0/, Key: hasXMP, Prefix: iio, value: True
//Namespace: http://ns.apple.com/HDRGainMap/1.0/, Key: HDRGainMapVersion, Prefix: HDRGainMap, value: 131072
//Namespace: http://ns.apple.com/HDRGainMap/1.0/, Key: HDRGainMapHeadroom, Prefix: HDRGainMap, value: 3.586325

I want to create a new CGImageMetadata and tags. But when it comes to the HDR tags. It always fails to add to metadata.

let tag = CGImageMetadataTagCreate(
        "http://ns.apple.com/HDRGainMap/1.0/" as CFString,  
        "HDRGainMap" as CFString,    
        "HDRGainMapHeadroom" as CFString,        
        .default,               
        3.56 as CFNumber                   
    ) 
let path = "HDRGainMap:HDRGainMapVersion" as CFString
let success = CGImageMetadataSetTagWithPath(mutableMetadata, nil, path, tag)// always false

The hasXMP works fine. Is HDR a private dict for Apple?

Upvotes: 0

Views: 30

Answers (1)

Jiayuan Li
Jiayuan Li

Reputation: 1

I had the same issue.

I am trying to create an empty metadata by CGImageMetaDataSetValueWithPath and set values for HDRGainMapHeadroom and HDRGainMapVersion. Nothing was written in.

Perhaps your guess is true, and the name space for HDR is private.

But anyway, this way might help you:

new_meta = CGImageMetadataCreateMutableCopy(metadata)

While metadata is from one HDR file from your iPhone, then this works:

CGImageMetaDataSetValueWithPath(new_meta, nil, "your_key", "your_value")

Upvotes: 0

Related Questions